题目:

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. ADO.NET中的DataReader详解

    使用特性 原理图 PS:Read()使指针下移,同时销毁上一条.所以SqlDataReader是只进的. GetValue()是找当前行中的列 SqlDataReader()特性. 1)只进的  上面 ...

  2. 实例分析ELF文件动态链接

    参考文献: <ELF V1.2> <程序员的自我修养---链接.装载与库>第6章 可执行文件的装载与进程 第7章 动态链接 <Linux GOT与PLT> 开发平台 ...

  3. Ubuntu下fcitx安装。(ibus不会用)

    1 安装命令: sudo apt-get install fcitx-table-wbpy 2 然后将语言支持中的键盘输入方式系统选项,选为fcitx. 3 注销系统后即可使用.

  4. 《第一行代码--Android》阅读笔记之Activity

    1.BaseActivity里面可以干什么 定义一个Context定义一个TAG 记录当前的Activity名字getClass().getSimpleName(); 2.Activity里面的几个重 ...

  5. 关于frameset中指定区域回退的实现

    指定区域(Frame)的回退,网上大都写的是用  window.parent.window.mainFrame.rightFrame.history.back();来进行回退,但是我这边就是不行,一直 ...

  6. 关于垃圾回收(GC和Marshal)有感

    最近做一个挂机软件.要求是挂个三四天没事,不会报错.开始的时候都是顺利的,所有步骤都是可以ok.但是当测试运行的是就出现问题了,内存居然会在一个Task跑完之后暴涨几M的内存.开了一台测试机测试了一天 ...

  7. 与谷歌测试工程师的对话 - from Google Testing Blog

    Conversation with a Test Engineer by Alan Faulner Alan Faulner谷歌的一名测试工程师,他工作在DoubleClick Bid Manager ...

  8. 仿SDWebImage

    仿SDWebImage 目标:模拟 SDWebImage 的实现 说明:整体代码与之前博客上的演练代码的基本一致,只是编写顺序会有变化! 在模仿 SDWebImage 之前,首先需要补充一个知识点:N ...

  9. 帮朋友 解决一道 LeetCode QJ上问题

    引言 对于刷题,自己是没能力的. 最经一个朋友同事考我一道数组题 . 也许能当面试分享吧. 娱乐娱乐. 事情的开始是这样的. 前言 题目 截图 大概意思 是 在一个 数组中,找出其中两个不重复出现的元 ...

  10. C/C++ 对常见字符串库函数的实现

    在c中的string.h头文件中存在很多对字符串进行操作的函数,利用这些函数可以方便的对字符串进行操作.下面将对常见的字符串函数进行解释和实现. strcpy 函数原型:char* _strcpy(c ...