101. Symmetric Tree
2016-03-18 21:04
267 查看
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:
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what
read more on how binary tree is serialized on OJ.
Subscribe to see which companies asked this question
Show Tags
dfs算法
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what
"{1,#,2,3}"means? >
read more on how binary tree is serialized on OJ.
Subscribe to see which companies asked this question
Show Tags
dfs算法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private boolean isSym (TreeNode left,TreeNode right){ 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)); } public boolean isSymmetric(TreeNode root) { if(root==null)return true; return isSym(root.left,root.right); } }
相关文章推荐
- 聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut
- 110. Balanced Binary Tree
- 在C++中实现委托(Delegate)
- 106. Construct Binary Tree from Inorder and Postorder Traversal | Java最短代码实现
- strrev()函数会修改参数原本的值
- A*搜寻算法(A星算法)
- 【codevs 1080~1082】线段树练习重做
- 极路由的“802.1x手机号登陆wifi”插件,电脑连接wifi方法
- python-Day8
- 21. Merge Two Sorted Lists
- java编程之weblogic(12.2.1)环境下jndi与数据源(data source)的部分操作(引入weblogic.jar)
- 面试宝典
- python正则表达式
- UVA 3027 Corporative Network 带权并查集、
- 231. Power of Two
- python 分布式进程
- 【五校联考2day2】WYF的盒子
- adb 一次性push多个apk
- 简单理解Android的Fragement组件
- ctf中杂项试题解析