首页 > 其他 > 详细

Symmetric Tree

时间:2014-02-06 16:34:28      阅读:386      评论:0      收藏:0      [点我收藏+]

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
bubuko.com,布布扣
 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

Symmetric Tree

原文:http://www.cnblogs.com/krunning/p/3538794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!