Search This Blog

Thursday, December 27, 2012

LeetCode:Symmetric Tree

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
Note: Bonus points if you could solve it both recursively and iteratively.

Recursive Solution:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSymmetric(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(root==null) return true;
        return isSymmetric(root.left,root.right);
    }
    
    public boolean isSymmetric(TreeNode a, TreeNode b){
        if(a==null) return b==null;
        if(b==null) return false;
        
        if(a.val!=b.val) return false;
        
        if(!isSymmetric(a.left,b.right)) return false;
        if(!isSymmetric(a.right,b.left)) return false;
        
        return true;
    }
}


Iterative Solution
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSymmetric(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        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;
    }
}

4 comments:

Anonymous said...

Beautiful recursive solution.

Anonymous said...

Why if(a == NULL) b= NULL?

Anonymous said...

because the two lines are like this:

if(a==null) return b==null;



so if a is NULL then we only need to see if b is NULL, we have two scenarios:
1. b is NULL, then it should return TRUE(both a and b are NULL, it is symmetric);
2. b is NOT NULL, then it should return FALSE(a is NULL but b is NOT NULL, it is NOT symmetric);

see the result is exactly same as the line "if(a==null) return b==null;"

this line is a bit hard to understand, maybe we can write it in a way easier to understand, like:

if (a == null && b == null) {
return true;
} else (a == null && b != null) {
return false;
}

but this is a bit verbose too, it is update the coder.

Anonymous said...

l.add(temp1.left);
what if temp1.left equals to null? I thought add() can not add a null value.