LeetCode: Symmetric Tree 解题报告
Symmetric Tree
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
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.
SOL 1 & 2:
两个解法,递归和非递归.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// solution 1:
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
} return isSymmetricTree(root.left, root.right);
} /*
* 判断两个树是否互相镜像
(1) 根必须同时为空,或是同时不为空
*
* 如果根不为空:
(1).根的值一样
(2).r1的左树是r2的右树的镜像
(3).r1的右树是r2的左树的镜像
*/
public boolean isSymmetricTree1(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
} if (root1 == null || root2 == null) {
return false;
} return root1.val == root2.val &&
isSymmetricTree(root1.left, root2.right) &&
isSymmetricTree(root1.right, root2.left);
} // solution 2:
public boolean isSymmetricTree(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
} if (root1 == null || root2 == null) {
return false;
} Stack<TreeNode> s1 = new Stack<TreeNode>();
Stack<TreeNode> s2 = new Stack<TreeNode>(); // 一定记得初始化
s1.push(root1);
s2.push(root2); while (!s1.isEmpty() && !s2.isEmpty()) {
TreeNode cur1 = s1.pop();
TreeNode cur2 = s2.pop(); if (cur1.val != cur2.val) {
return false;
} if (cur1.left != null && cur2.right != null) {
s1.push(cur1.left);
s2.push(cur2.right);
} else if (!(cur1.left == null && cur2.right == null)) {
return false;
} if (cur1.right != null && cur2.left != null) {
s1.push(cur1.right);
s2.push(cur2.left);
} else if (!(cur1.right == null && cur2.left == null)) {
return false;
}
} return true;
}
}
2015.1.20:
简化了递归的解法,root与root本身是一对对称树。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
} public boolean isMirror(TreeNode r1, TreeNode r2) {
if (r1 == null && r2 == null) {
return true;
} else if (r1 == null || r2 == null) {
return false;
} return r1.val == r2.val
&& isMirror(r1.left, r2.right)
&& isMirror(r1.right, r2.left);
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsSymmetric.java
LeetCode: Symmetric Tree 解题报告的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
随机推荐
- maven 找不到或无法加载主类
maven 找不到或无法加载主类 CreateTime--2018年4月19日22:58:14 Author:Marydon 1.情景还原: 在maven管理的web项目中,单独运行Java类报错 ...
- HDOJ 4699 Editor 栈 模拟
用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- tar压缩解压
把所有jpg文件和temp文件夹压缩到 jpg.tar.gz 压缩文件中 tar -czvf jpg.tar.gz *.jpg temp 把jpg.tar.gz 解压缩当前文件中 tar -xzv ...
- html+css+js实现网页拼图游戏
代码地址如下:http://www.demodashi.com/demo/14449.html 项目描述 使用 html+js+css 实现一个网页拼图游戏,可支持简单,中等,困难三种难度. 演示效果 ...
- TaskMapper
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- Java开源内容管理CMS系统J4CMS集成到JTM
JTM是Win32下绿色免费的JDK + Tomcat + MySQL环境集成工具. 通过JTM用户无需对JDK.Tomcat.MySQL进行不论什么安装和配置就可以迅速搭建支持JSP + MySQL ...
- 架构-LAMP特级学习(网站加速解决方案)
1.Squid代理缓存技术 2.页面静态化缓存技术 3.Memcache.Redis等缓存服务器 4.Sphinx搜索加速
- matplot模块中的pylab
pylab的目的 Pylab combines the functionality of pyplot with the capabilities of NumPy in a single names ...
- Total Commander:文件管理工具,共享软件
http://xbeta.info/tc/ Total Commander V8.0:文件管理工具,共享软件,适用于Windows® 95/98/ME/NT/2000/XP/Vista/7 和 Win ...
- java开发_eclipse导出为war文件,热部署到tomcat运行总结[转]
在Myeclipse中,我们很容易做到这一步:把一个web项目生成war文件 其实在eclipse中,实现这样的功能,也是很简单的. 下面就看一下是怎样操作的吧! 新建一个web项目: 取名为:ecl ...