Symmetric Tree

有个问题,就是不能和same tree一样光看root 做recursive

而是必须比较左右节点,因为对称是指整个树

否则就会出现 1 2 2 # 3 3

public class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null) return true;
        
        return isSymmetric(root.left, root.right) ;
    }
    private boolean isSymmetric(TreeNode r, TreeNode l){
        if(r==null) return l==null;
        if(l==null) return r==null;
        if(r.val!=l.val) return false;
        return isSymmetric(l.left,r.right) && isSymmetric(l.right,r.left);
    }
}

 

posted @ 2015-06-10 03:16  世界到处都是小星星  阅读(115)  评论(0编辑  收藏  举报