[LintCode] Identical Binary Tree 相同二叉树
Check if two binary trees are identical. Identical means the two binary trees have the same structure and every identical position has the same value.
1 1
/ \ / \
2 2 and 2 2
/ /
4 4
are identical.
1 1
/ \ / \
2 3 and 2 3
/ \
4 4
are not identical.
LeetCode上的原题,请参见我之前的博客Same Tree。
解法一:
class Solution {
public:
/**
* @aaram a, b, the root of binary trees.
* @return true if they are identical, or false.
*/
bool isIdentical(TreeNode* a, TreeNode* b) {
if (!a && !b) return true;
if (!a || !b) return false;
return a->val == b->val && isIdentical(a->left, b->left) && isIdentical(a->right, b->right);
}
};
解法二:
class Solution {
public:
/**
* @aaram a, b, the root of binary trees.
* @return true if they are identical, or false.
*/
bool isIdentical(TreeNode* a, TreeNode* b) {
stack<TreeNode*> s1, s2;
if (a) s1.push(a);
if (b) s2.push(b);
while (!s1.empty() && !s2.empty()) {
TreeNode *t1 = s1.top(); s1.pop();
TreeNode *t2 = s2.top(); s2.pop();
if (t1->val != t2->val) return false;
if (t1->left) s1.push(t1->left);
if (t2->left) s2.push(t2->left);
if (s1.size() != s2.size()) return false;
if (t1->right) s1.push(t1->right);
if (t2->right) s2.push(t2->right);
if (s1.size() != s2.size()) return false;
}
return s1.empty() && s2.empty();
}
};
[LintCode] Identical Binary Tree 相同二叉树的更多相关文章
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- LintCode: Identical Binary Tree
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III
Given a k-ary tree, find the length of the longest consecutive sequence path. The path could be star ...
随机推荐
- Ubuntu下Chromium for Android 源码的编译
转自:http://blog.csdn.net/leer168/article/details/9146689 一.环境Ubuntu10.4.4 -desktop-amd64 + VMware Wor ...
- 为什么接口类型可以直接new?
Runnable rn = new Runnable() { public void run() { } }; 实际相当于,jdk会自动生成一个匿名内部类,完成职责: class Anomymous ...
- 【MySQL 安装过程1】顺利安装MySQL完整过程
一.MySQL Sever的安装 1.开始安装: 2.这里就要开始注意,端口号我们的my SQL端口号为3306 3.下面要输入用户名和用户密码.注意,帐号密码 都是 root. 4.下面的最后一页 ...
- mysql一些有用的链接
1.mysql安装:http://jingyan.baidu.com/article/f79b7cb35c0f439144023e38.html
- MPAndroidChart饼图属性及相关设置
公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...
- SpringMyBatis解析2-SqlSessionFactoryBean
通过分析整合示例中的配置文件,我们可以知道配置的bean其实是成树状结构的,而在树的最顶层是类型为org.mybatis.spring.SqlSessionFactoryBean的bean,它将其他相 ...
- Ipython console in Spyder stuck on “connecting to kernel”
简短地记录下,今天排除的spyder的BUG, 现象:打开Spyder时其他正常,但是Ipython console 不能正常获取到kernel,一直转圈,显示“connecting to kerne ...
- TestNg依赖详解(三)------灵活的文件配置依赖
配置型的依赖测试,让依赖测试不局限于测试代码中,在XML文件中进行灵活的依赖配置 原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong Javaco ...
- 字体和壁纸合并后再更改壁纸--《用delphi开发共享软件》-15.2桌面提示器
procedure TFrmPlay.mnDeskPicClick(Sender: TObject); Var s:String; i:Integer; begin //s:=Path+'SetPic ...
- POJ2135 Farm Tour(最小费用最大流)
题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...