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
 1 public class Solution {
 2     public boolean isSymmetric(TreeNode root) {
 3         if(root==null)
 4         return true;
 5         return is(root.left,root.right);
 6     }
 7     public boolean is(TreeNode left,TreeNode right){
 8         if(left==null)
 9             return right==null;
10         if(right==null)
11             return left==null;
12         if(right.val!=left.val)
13             return false;
14         if(!is(left.left,right.right))
15             return false;
16         if(!is(left.right,right.left))
17             return false;
18         return true;
19     }
20     
21 }
View Code

 

posted @ 2014-02-06 14:57  krunning  阅读(98)  评论(0编辑  收藏  举报