题解 | #对称的二叉树#

对称的二叉树

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 traversal(pRoot.left, pRoot.right);
    }
    boolean traversal(TreeNode root1, TreeNode root2){
        if(root1==null&&root2==null){
            return true;
        }
        if(root1!=null&&root2==null){
            return false;
        }
        if(root1==null&&root2!=null){
            return false;
        }
        if(root1.val!=root2.val){
            return false;
        }
        return traversal(root1.left, root2.right)&&traversal(root1.right,root2.left);
    }
}
全部评论

相关推荐

头像 会员标识
昨天 20:45
已编辑
蚌埠坦克学院 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务