题目:

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的更多相关文章

  1. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  2. 【Maximum Depth of Binary Tree 】cpp

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  3. 【Minimum Depth of Binary Tree】cpp

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  4. 【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 ...

  5. 【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 ...

  6. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  7. 【Recover Binary Search Tree】cpp

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  8. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  9. 【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 ...

随机推荐

  1. jquery 分页控件功能

      <script>        //分页         function getPageNum(num) {             $("#PageNum ul" ...

  2. 用Lambda表达式操作List集合

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. 转载:T-SQL语句大全

    一.基础 1.创建数据库 CREATE DATABASE database-name 2.删除数据库 drop database dbname 3.备份数据库 --- 创建 备份数据的 device ...

  4. PHP代码优化的53个细节

    PHP代码优化的53个细节,常见而重要的php优化策略. 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一 ...

  5. Nob畅想在线教育

    1.社交网络的课堂实时互动 老师上课,每当和同学们互动时大家下边总是保持沉默,低着头,几乎每人拿着一部手机在看,还有pad等. 张星老师的课算是好一点,学生可以抬着头然后手下边捏着手机,时不时低头看一 ...

  6. Java数字处理

    给出一个不多于5位的正整数,要求如下: (1)求出该数是几位数. (2)分别打印出每一位数字. (3)按照逆序打印出各位数值. 按照以上要求,首先得用户从键盘输入一个不多于5位的正整数,可以用Syst ...

  7. php 判断table 是否存在 根据返回值继续下一步的操作

    根据sql命令创建数据库或者数据表时候,判断库或者表是否存在比较重要. //要创建的表是否已经存在 function isHaveTable( $dbName,$tableN, $con)  //数据 ...

  8. .NET软件汉化小实例

    Author:KillerLegend Date:2014.6.18 From:http://www.cnblogs.com/killerlegend/p/3795577.html 好的,今天我们来汉 ...

  9. 第四节:监视AppDomain

    宿主应用程序可监视AppDomain消耗的资源.有的宿主根据这种信息判断一个AppDomain的内存或CPU消耗是否超过了应有的水准,并强制卸载一个AppDomain. 还可以利用监视来比较不同算法的 ...

  10. 说说用C语言求根的那些事儿

    C语言--求根:计算机只识别0和1,那么问题来了,作为计算工具如何解决数学问题?其实,计算机是死东西,都是程序员用计算机的的思维去加数学公式计算数学题的.听起来好高端的样子,其实啊,也就那么回事儿, ...