101. Symmetric Tree
2016-02-14 01:10
417 查看
树相关的问题一般都是递归
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)return true;
return isMirror(root.left,root.right);
}
public boolean isMirror(TreeNode p,TreeNode q){
if(p==null&&q==null)return true;
if(p==null||q==null)return false;
if(p.val==q.val&&isMirror(p.left,q.right)&&isMirror(p.right,q.left))return true;
else return false;
}
}
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)return true;
return isMirror(root.left,root.right);
}
public boolean isMirror(TreeNode p,TreeNode q){
if(p==null&&q==null)return true;
if(p==null||q==null)return false;
if(p.val==q.val&&isMirror(p.left,q.right)&&isMirror(p.right,q.left))return true;
else return false;
}
}
相关文章推荐
- HDU 5626 Clarke and points 平面两点曼哈顿最远距离
- 110. Balanced Binary Tree
- jquery tmpl 详解
- 1040. Longest Symmetric String (25)
- 动态规划入门练习
- 【POJ 1035】Spell checker
- MOOC Machine Learning 作业交流帖5
- POJ 1094(拓扑排序)
- 1039. Course List for Student (25)
- 算法导论例程——红黑树
- HUST 1010 The Minimum Length(最小循环节)
- 【ZOJ 1221】Risk
- MOOC Machine Learning 作业交流帖4
- 数组中添加数据后并排序显示出来
- 最短路算法floyd
- js验证输入,自定义错误提示
- [回文自动机]bzoj2342: [Shoi2011]双倍回文
- 集群监控_Ganglia使用入门
- 机器学习实践-Ch02 kNN分类算法
- MOOC Machine Learning 作业交流帖3