题解 | #判断二叉树是否对称#

判断二叉树是否对称

http://www.nowcoder.com/practice/1b0b7f371eae4204bc4a7570c84c2de1

二叉树的题向来都是一看就会,一写就废
public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return bool布尔型
     */
    public boolean isSymmetric (TreeNode root) {
        // write code here
        return root == null || isSymmetric(root.left,root.right);
    }
    public boolean isSymmetric(TreeNode left,TreeNode right){
        if(left == null && right == null)
            return true;
        if(left == null || right == null || left.val != right.val)
            return false;
        return isSymmetric(left.left,right.right) && isSymmetric(left.right,right.left);
    }
}


全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务