美文网首页
对称的二叉树

对称的二叉树

作者: 稀饭粥95 | 来源:发表于2018-08-30 00:20 被阅读7次

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

    public class Solution {
        boolean same(TreeNode pLeft,TreeNode pRight){
            if(pLeft==null&&pRight==null){
                return true;
            }
            if(pLeft!=null&&pRight!=null){
                return pLeft.val==pRight.val
                        &&same(pLeft.left,pRight.right)
                        &&same(pLeft.right,pRight.left);
            }
            return false;
        }
        
        boolean isSymmetrical(TreeNode pRoot)
        {
            if(pRoot==null){
                return true;
            }
            return same(pRoot.left,pRoot.right);
        }
    }
    

    相关文章

      网友评论

          本文标题:对称的二叉树

          本文链接:https://www.haomeiwen.com/subject/tuvlwftx.html