Chinaunix首页 | 论坛 | 博客
  • 博客访问: 256085
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-09-26 15:19:31

//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
public class SymmetricTree {


public static void main(String[] args) {
// TODO Auto-generated method stub


}
public boolean isSymmetric(TreeNode root) {
        if(root==null)
        return true;
        return isSym(root.left,root.right);
    }
private boolean isSym(TreeNode left, TreeNode right) {
// TODO Auto-generated method stub
if(left==null&&right==null)
return true;
if(left==null||right==null){
return false;
}
if(left.val!=right.val)
return false;
return isSym(left.left, right.right)&&isSym(left.right,right.left);
}


}

阅读(617) | 评论(0) | 转发(0) |
0

上一篇:zigzagLevelOrder

下一篇:Validate Binary Search Tree

给主人留下些什么吧!~~