题解 | #对称的二叉树#

对称的二叉树

http://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 isSymmetrical(TreeNode pRoot) {
        if(pRoot == null){
            return true;
        }
        return isSymmetrical(pRoot.left, pRoot.right);
    }

    boolean isSymmetrical(TreeNode root1, TreeNode root2) {
        if(root1 == null && root2 == null){
            return true;
        }
        if(root1 == null || root2 == null){
            return false;
        }

        return (root1.val == root2.val) && isSymmetrical(root1.left, root2.right) && isSymmetrical(root1.right, root2.left);
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
2024-12-23 06:50
门头沟学院 Java
给点吧求求了:3点发的帖子,害怕😰
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务