题目:

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. linux下快速删除大量文件

    昨天遇到一个问题,在Linux中有一个文件夹里面含有大量的Cache文件(夹),数量级可能在百万级别,使用rm -rf ./* 删除时间慢到不可接受.Google了一下,查到了一种方法,试用了下确实比 ...

  2. WWDC————苹果全球开发者大会

    WWDC:Apple Worldwide Developers Conference(苹果全球开发者)的简称,每年定期由苹果公司(Apple Inc.)在美国举办.大会主要的目的是让苹果公司向研发者们 ...

  3. sqlserver 保留小数方法

    1. 使用 Round() 函数,如 Round(@num,2)  参数 2 表示 保留两位有效数字. 2. 更好的方法是使用 Convert(decimal(18,2),@num) 实现转换,dec ...

  4. 未能加载文件或程序集“Oracle.Web, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342”或它的某一个依赖项

    当前系统环境描述: Win7x64+VS2012+IIS7 当前情况描述: 发布Web服务,在浏览的时候出现以下问题:未能加载文件或程序集“Oracle.Web, Version=2.112.1.0, ...

  5. shell脚本入门

    什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...

  6. mouseout以及mouseover

    两个图层 红色图层代表outside蓝色图层代表inside dom结构如下 <div id="outside">      <div id="insi ...

  7. css font-family 字体全介绍,\5b8b\4f53 宋体 随笔

    font-family采用一种"回退"的形式来保存字体,可以写若干种字体.当第一种字体浏览器不支持的时候,会找第二种字体,一次类推. font-family字体分为两类: 特殊字体 ...

  8. 【easyui】--普通js中获取easyui中分页信息(page,pageSize等)

    对于datagrid,获取其分页信息: 方法: var pageopt = $('#list_data').datagrid('getPager').data("pagination&quo ...

  9. php框架学习的步骤

    一,选择一个合适的php框架 在国内,使用zf,ci和tp框架的人比较多,新手可以从中选一个去学习,新手不建议一开始就去学习zf,功力还不够深,学习zf会让你更迷茫. 二,选定一个php框架之后,如何 ...

  10. delphi 更改不了窗体的标题

    delphi定义变量名千万要注意,不能和关键字同名,今天我无意间定义了一个caption的变量  导致我怎么都不能修改窗的标题.