题解 | #对称的二叉树#

对称的二叉树

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

    public static boolean isSame(TreeNode t1, TreeNode t2){
        if(t1 == null ^ t2 == null){
            return false;
        }
        if(t1==null && t2 == null){
            return true;
        }

        boolean b1 = t1.val == t2.val;
        boolean b2 = isSame(t1.left,t2.right);
        boolean b3 = isSame(t1.right,t2.left);

        return b1 && b2 && b3;
    }
}

全部评论

相关推荐

吃不饱的肱二头肌很想退休:tnnd 我以为选妹子呢,亏我兴高采烈的冲进来😠
投递快手等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务