题解 | #对称的二叉树#

对称的二叉树

https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    boolean check(TreeNode root1, TreeNode root2){
        if(root1 == null && root2 == null){
            return true;
        }
        if(root1 == null && root2 != null || root1 != null && root2 == null || root1.val != root2.val){
            return false;
        }
        return check(root1.left, root2.right) && check(root1.right, root2.left);
    }
    boolean isSymmetrical(TreeNode pRoot) {
        //判断二叉树对称
        //对称的要求是,每一层,最左边的节点值等于最右边的节点值
        //左右分开来看
        //左树的最右边,是否等于右树的最左边
        if(pRoot == null){
            return true;
        }
        
        return check(pRoot,pRoot);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务