【Same Tree】cpp
题目:
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if ( !p && !q ) return true;
stack<TreeNode *> sta_p,sta_q;
if (p) sta_p.push(p);
if (q) sta_q.push(q);
while ( !sta_p.empty() && !sta_q.empty() ){
TreeNode * tmp_p = sta_p.top();
sta_p.pop();
TreeNode * tmp_q = sta_q.top();
sta_q.pop();
// node val
if ( tmp_p->val==tmp_q->val ){
// right child
if ( tmp_p->right && tmp_q->right ){
sta_p.push(tmp_p->right);
sta_q.push(tmp_q->right);
}
else if ( !tmp_p->right && !tmp_q->right )
{}
else{ return false; }
// left child
if ( tmp_p->left && tmp_q->left ){
sta_p.push(tmp_p->left);
sta_q.push(tmp_q->left);
}
else if ( !tmp_p->left && !tmp_q->left )
{}
else{ return false; }
}
else { return false; }
}
return sta_p.empty() && sta_q.empty();
}
};
tips:
二叉树先序遍历。
1. 比较节点val
2. 比较节点right child
3. 比较节点left child
========================
学习了一个更简洁版的代码,主要简洁的地方是入栈时候不需要判断为NULL。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
stack<TreeNode *> sta_p,sta_q;
sta_p.push(p);
sta_q.push(q);
while ( !sta_p.empty() && !sta_q.empty() )
{
p = sta_p.top();
sta_p.pop();
q = sta_q.top();
sta_q.pop();
if ( !q && !p ) continue;
if ( !q || !p ) return false;
if ( p->val!=q->val ) return false;
sta_p.push(p->right);
sta_p.push(p->left);
sta_q.push(q->right);
sta_q.push(q->left);
}
return sta_p.empty() && sta_q.empty();
}
};
==============================================
第二次过这道题,第一次没有AC:有个思维陷阱,如果线序遍历输出的顺序相同,两棵树不一定完全相等。改了一次后AC了。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
stack<TreeNode*> sta1;
if (p) sta1.push(p);
stack<TreeNode*> sta2;
if (q) sta2.push(q);
while ( !sta1.empty() && !sta2.empty() )
{
TreeNode* n1 = sta1.top(); sta1.pop();
TreeNode* n2 = sta2.top(); sta2.pop();
if ( n1->val != n2->val ) return false;
// right child
if ( n1->right && n1->right )
{
sta1.push(n1->right);
sta2.push(n2->right);
}
else if ( !n1->right && !n2->right )
{}
else
{
return false;
}
// left child
if ( n1->left && n1->left )
{
sta1.push(n1->left);
sta2.push(n2->left);
}
else if ( !n1->left && !n2->left )
{}
else
{
return false;
}
}
return sta1.empty() && sta2.empty();
}
};
再补上一个递归版的。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q)
{
if ( p && q )
{
if ( p->val==q->val )
{
return Solution::isSameTree(p->left, q->left) && Solution::isSameTree(p->right, q->right);
}
else
{
return false;
}
}
else if ( !p && !q )
{
return true;
}
else
{
return false;
}
}
};
【Same Tree】cpp的更多相关文章
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- 【Maximum Depth of Binary Tree 】cpp
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【Minimum Depth of Binary Tree】cpp
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 【Convert Sorted List to Binary Search Tree】cpp
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- 【Convert Sorted Array to Binary Search Tree】cpp
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- 【Validate Binary Search Tree】cpp
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【Recover Binary Search Tree】cpp
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- 【Lowest Common Ancestor of a Binary Search Tree】cpp
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
随机推荐
- mysql在生产环境下有大量锁表,又不允许重启的情况下的处理办法
mysql在生产环境下有大量锁表,又不允许重启的情况下的处理办法 满头大汗的宅鸟该怎么办呢? mysql -u root -e "show processlist"|grep -i ...
- C#关于Sort排序问题
1.在集合中用Sort对集合元素进行排序 List<,,,,}; tmp.Sort((x, y) => -x.CompareTo(y)); Console.WriteLine(tmp); ...
- 5.21_启程日本二面_1 vs 1
昨天上午刚群面完,晚上7点左右就接到了电话.面试官就两位菇凉,看来她们也是很辛苦.今天下午3点 1 vs 1,在一家咖啡店里,主要是询问下去日本的意愿是否足够强烈.太老实,这里实话实说,也没有表现出非 ...
- WPF: 读取XPS文件或将word、txt文件转化为XPS文件
读取XPS格式文件或将doc,txt文件转化为XPS文件,效果图如下: 1.XAML页面代码: <Window x:Class="WpfWord.MainWindow" xm ...
- sqoop导出工具
sqoop的导出工具是把HDFS上文件中的数据导出到mysql中 mysql中的表 现在在linux上创建一个文件,并把这个文件上传到hdfs上 cat person.txt ,no7, ,no8, ...
- HTML5+CSS3前端开发资源整合
HTML5+CSS3前端开发资源整合 推个广告 个人网站:http://www.51pansou.com HTML5视频下载:HTML5视频 HTML5源码下载:HTML5源码 meta相关: & ...
- SequoiaDB(巨杉数据库)(社区版)安装配置使用图解
SequoaiDB是一款新型企业级分布式非关系型数据库,提供了基于PC服务器的大规模集群数据平台.作为全球第一家企业级文档式 NoSQL分布式数据库,为用户提供了一个高扩展性.高可用性.高性能.易维护 ...
- python 面向对象、特殊方法与多范式、对象的属性及与其他语言的差异
1.python 面向对象 文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html 1.__init__() 创建对 ...
- ode.js 版本控制 nvm 和 n 使用 及 nvm 重启终端失效的解决方法
今天的话题包括2个部分 node.js 下使用 nvm 或者 n 来进行版本控制 nvm 安装node.js 版本后,重启终端 node , npm 环境变量失效 第一部分 用什么来管理 node.j ...
- java 命令对象简单学习实现:
命令模式:首先我们要知道命令模式的基本定义:来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化.分离变化 ...