leetcode-101
2016-05-25 00:12
337 查看
题目:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean helper(TreeNode left, TreeNode right){
if(left == null || right == null)
{
if(left == right)
return true;
else
return false;
}
if(left.val != right.val)
return false;
else
return helper(left.left,right.right) && helper(left.right,right.left);
}
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
else
return helper(root.left,root.right);
}
}
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
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean helper(TreeNode left, TreeNode right){
if(left == null || right == null)
{
if(left == right)
return true;
else
return false;
}
if(left.val != right.val)
return false;
else
return helper(left.left,right.right) && helper(left.right,right.left);
}
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
else
return helper(root.left,root.right);
}
}
相关文章推荐
- ORACLE 里面的LIKE 语句的详细作用格式什么呀
- Git 操作标签的一些命令
- 王帅:深入PHP内核
- [leetcode]18. 4Sum
- 单片机 学习笔记(二)——简单小程序
- 15.NSTimer
- 程序员转管理之路
- 推荐算法的文献综述
- 【例题】【动规(分组)】NKOJ 1001 最小乘车费用
- Velt-0.2.6对Python远程调试的支持
- WinForm BaseClass类常用通用方法
- loadrunner在win10破解提示:Cannot save the license information because acceses to the registry is denied的解决办法
- 致在大学中感到迷茫或即将进入大学的你!
- Ant脚本初使用
- c++实验6-矩阵求和
- iOS开发小技巧--学会包装控件(有些view的位置由于代码或系统原因,位置或者尺寸不容易修改或者容易受外界影响)
- SecureCRT 7.3.4破解版(含注册机)
- AutoCloseable,Closeable和Flushable
- 基于LNMP实现动静分离,PHP+Memcached实现会话保持
- 多线程编程之block