zoukankan      html  css  js  c++  java
  • 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)

    1. 题目描述

    /**
        请实现一个函数,用来判断一颗二叉树是不是对称的。
        注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的
    */

    2. 递归

    思路:

    /**
        1.只要pRoot.left和pRoot.right是否对称即可
        2.左右节点的值相等且对称子树left.left, right.right; left.rigth,right.left也对称
    */

    代码:

    import java.util.*;
    public class Solution {
        boolean isSymmetrical(TreeNode pRoot){
            if(pRoot == null) return true;
            return isSymmetrical(pRoot.left, pRoot.right);
        }
        private boolean isSymmetrical(TreeNode left, TreeNode right) {
            if(left == null && right == null) return true;
            if(left == null || right == null) return false;
            return left.val == right.val //为镜像的条件:左右节点值相等
                    && isSymmetrical(left.left, right.right) //2.对称的子树也是镜像
                    && isSymmetrical(left.right, right.left);
        }
    }

    3. 非递归(深度遍历)

    思路

    /*
    * DFS使用stack来保存成对的节点
    * 1.出栈的时候也是成对的 ,
            1.若都为空,继续;
            2.一个为空,返回false;
            3.不为空,比较当前值,值不等,返回false;
    * 2.确定入栈顺序,每次入栈都是成对成对的,如left.left, right.right ;left.rigth,right.left
    */

    代码

    import java.util.*;
    public class Solution {
        boolean isSymmetrical(TreeNode pRoot){
            return isSymmetricalDFS(pRoot);
        }
        boolean isSymmetricalDFS(TreeNode pRoot){
            if(pRoot == null) return true;
            Stack<TreeNode> s = new Stack<>();
            s.push(pRoot.left);
            s.push(pRoot.right);
            while(!s.empty()) {
                TreeNode right = s.pop();//成对取出
                TreeNode left = s.pop();
                if(left == null && right == null) continue;
                if(left == null || right == null) return false;
                if(left.val != right.val) return false;
                //成对插入
                s.push(left.left);
                s.push(right.right);
                s.push(left.right);
                s.push(right.left);
            }
            return true;
        }
    }

    4. 非递归(广度遍历)

    思路:

    /*
    * BFS使用Queue来保存成对的节点,代码和上面极其相似
    * 1.出队的时候也是成对成对的
            1.若都为空,继续;
            2.一个为空,返回false;
            3.不为空,比较当前值,值不等,返回false;
    * 2.确定入队顺序,每次入队都是成对成对的,如left.left, right.right ;left.rigth,right.left
    */

    代码

    import java.util.*;
    public class Solution {
        boolean isSymmetrical(TreeNode pRoot){
            return isSymmetricalBFS(pRoot);
        }
        boolean isSymmetricalBFS(TreeNode pRoot)
        {
            if(pRoot == null) return true;
            Queue<TreeNode> s = new LinkedList<>();
            s.offer(pRoot.left);
            s.offer(pRoot.right);
            while(!s.isEmpty()) {
                TreeNode left= s.poll();//成对取出
                TreeNode right= s.poll();
                if(left == null && right == null) continue;
                if(left == null || right == null) return false;
                if(left.val != right.val) return false;
                //成对插入
                s.offer(left.left);
                s.offer(right.right);
                s.offer(left.right);
                s.offer(right.left);
            }
            return true;
        }
    }
  • 相关阅读:
    转载Dockerfile 中 RUN, CMD, ENTRYPOINT 的区别
    在linux上通过ssh使用github
    dns服务
    centos6 free 和 centos 7的free 的差异与对比
    无重复字符的最长子串
    go get命令在go mod目录下与正常目录执行的区别
    安装git
    转载 筛子算法之golang实现求素数解析
    Go语言基础之并发
    go之无缓冲channel(通道)和有缓冲channel(通道)
  • 原文地址:https://www.cnblogs.com/haimishasha/p/11520978.html
Copyright © 2011-2022 走看看