leetcode

Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

    1
   / \
  2   2
   \   \
   3    3

这道题的关键就是判断left.left与right.right以及left.right与right.left是否相同。

public class SymmetricTree {
    public boolean isSymmetric(TreeNode root) {
        if (root==null) {
            return true;
        }

        return isSymmetric(root.left, root.right);
    }


    public boolean isSymmetric(TreeNode left, TreeNode right) {
        if (left==null) {
            return right==null;
        }
        //left is not null
        if (right==null) {
            return false;
        }        
        //both left and right are not null
        if (left.val != right.val) {
            return false;
        }

        return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);

    }

    public boolean isSymmetricIter(TreeNode root) {
        if(root==null) return true;
        LinkedList<TreeNode> l = new LinkedList<TreeNode>(),
                            r = new LinkedList<TreeNode>();
        l.add(root.left);
        r.add(root.right);
        while(!l.isEmpty() && !r.isEmpty()){
            TreeNode temp1=l.poll(), temp2=r.poll();
            if(temp1==null && temp2!=null || temp1!=null && temp2==null)
                return false;
            if(temp1!=null){
                if(temp1.val!=temp2.val) return false;
                l.add(temp1.left);
                l.add(temp1.right);
                r.add(temp2.right);
                r.add(temp2.left);
            }
        }
        return true;
    }
}