《剑指Offer》28. 对称的二叉树

题目链接

牛客网

题目描述

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

解题思路

主要是判断两颗二叉树是不是镜像

public class Solution {
   
    public boolean isSymmetrical(TreeNode pRoot) {
   
        if (pRoot==null) return true;
        return isMirrorTrees(pRoot.left, pRoot.right);
    }
    private boolean isMirrorTrees(TreeNode tree1, TreeNode tree2) {
   
        if (tree1==null && tree2==null) return true;
        if (tree1==null || tree2==null) return false;
        if (tree1.val != tree2.val) return false;
        return isMirrorTrees(tree1.left, tree2.right) && isMirrorTrees(tree1.right, tree2.left);
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务