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 ...
随机推荐
- Javascript获取当月的天数
var d = new Date(); var curMonthDays = new Date(d.getFullYear(), (d.getMonth() + 1), 0).getDate(); a ...
- Start-Sleep 帮助信息
如下说明是翻译: help Start-Sleep 产生的帮助信息.译者: Edengundam(马涛) Start-Sleep 大纲使shell, 脚本, 或运行空间的活动挂起指定的时间. 语法St ...
- Chrome 编译错误汇总
由于各种你懂的原因,訪问google的服务总是出错,先是hosts不工作.代理也不好使,最后最终能够短暂訪问了.我的版本号还是採用svn维护的,直接svn update也不行.试试git吧,一晚上才下 ...
- mysql中内存的使用与分配
mysql的内存分配,是调优的重中之重,所以必须搞清楚内存是怎么分配的 mysql> show global variables like '%buffer%'; +-------------- ...
- JavaScript中的call、apply、bind方法的区别
在JavaScript 中,this的指向是动态变化的,很可能在写程序的过程中,无意中破坏掉this的指向,所以我们需要一种可以把this的含义固定的技术,于是就有了call,apply 和bind这 ...
- springmvc多视图配置
http://blog.csdn.net/yaerfeng/article/details/23593755
- HDUOJ----John
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- ORM轻量级框架---ActiveAndroid
ORM即Object-Relational Mapping,对象关系映射.简单理解就是把我们Java的对象与数据库里面的记录进行映射,可以把实体对象持久化到数据库中,也能把查询到的记录映射成Java对 ...
- PLSQL_SQL Loader的概念和用法(概念)
2014-06-08 Created By BaoXinjian
- Python tuple 函数
描述 Python 元组 tuple() 函数将列表转换为元组. 语法 以下是 tuple 的语法: tuple( seq ) 参数 seq -- 要转换为元组的序列. 返回值 返回元组. 实例 以下 ...