【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 ...
随机推荐
- .NET中的字符串你了解多少?
字符串的特性 1.不可变性 由于字符串是不可变的的,每次修改字符串,都是创建了一个单独字符串副本(拷贝了一个字符串副本).之所以发生改变只是因为指向了一块新的地址. ps: ...
- 基于CSS+dIV的网页层,点击后隐藏或显示
一个基于CSS+dIV的网页层,用JavaScript结合Input按钮进行控制,点击后显示或隐藏,网页上常用到的特效之一,实用性较强,相信对大家的前端设计有帮助. <!DOCTYPE html ...
- Windows2003屏蔽IP
1.打开本地安全策略 2.创建新的IP策略 去掉勾选向导 我们编辑 直接右键指派 指派可以看出来生效...网络已经不通了
- Eclipse插件推荐:UCDetector: Unnecessary Code Detector
正如其名,检查不必要的代码. 下载地址为:http://sourceforge.net/projects/ucdetector/files/latest/download?source=files 官 ...
- 【转】利用DCC32实现命令行批量编译
*.dof [Compiler] A=1 B=0 C=1 D=1 E=0 F=0 G=1 H=1 I=1 J=1 K=0 L=1 M=0 N=1 O=1 P=1 Q=0 R=0 S=0 T=0 U=0 ...
- cxGrid使用汇总(一)
1. 去掉cxGrid中台头的Box 解决:在tableview1的ptionsview的groupbybox=false; 2.统计功能 解决:(1) tableview 1. tableview1 ...
- jQuery学习笔记(6)--复选框控制表格行高亮
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- SPARK 数据统计程序性能优化。
昨天写完R脚本 没测试就发到博客里, 结果实际运行发现很慢,运行时间在2小时以上, 查看spark控制台, 大量时间消耗在count上, 产生的stage多大70多个 . 分析原因. 1 selec ...
- jquery.validate新的写法(jquery.validate1.13.js)
<script src="../js/jquery.js"></script> <script src="../js/jquery.vali ...
- DISP_FUNCTION用法
DISP_FUNCTION(theClass, pszName, pfnMember, vtRetVal, vtsParams ) 参数 theClass 类名. pszName 扩展函数名. pfn ...