(Tree) 101. Symmetric Tree
2016-04-12 21:06
309 查看
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:
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 isSymmetric(TreeNode root) { return isSymmetric(root,root); } public boolean isSymmetric(TreeNode t1,TreeNode t2) { if(t1==null && t2==null) return true; if(t1==null || t2==null) return false; return(t1.val==t2.val && isSymmetric(t1.left,t2.right) && isSymmetric(t1.right,t2.left)); } }
相关文章推荐
- 万年历
- HDU 4587 (割点)
- 学习android第六天
- 风险管理、收尾管理、知识产权管理
- 上机报告3-1个人所得税计算器
- 面试题4-替换空格
- java.text.format
- centos7安装后windows启动菜单修复
- poj 2653 Pick-up sticks
- Solr4.7实现LBS(地理位置搜索)
- Atitit.prototype-base class-based 基于“类” vs 基于“原型”
- hdoj--1075 What Are You Talking About(Trie or map)
- 剑指offer系列之42:翻转单词序列
- angular2 Displaying
- Atitit.prototype-base class-based 基于“类” vs 基于“原型”
- 操作整数
- iOS动态性(二):运行时runtime初探(强制获取并修改私有变量,强制增加及修改私有方法等)
- Eclipse中Pydev使用import时出现报错
- Atitit.prototype-base class-based 基于“类” vs 基于“原型”
- Android:Fragment 与 ViewPager的配合使用