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.

二叉树相同:一、结构相同;二、对应节点上的值相同。

思路:层次遍历,维护两个队列,判断对应节点的值是否相等。值得注意的有:一、节点均为空时,再次循环取队首元素,有两种情况,i)q、p就为空,则不满足再次循环条件时,此时q==p为空,两树相同;ii) 到叶节点时,将其左右孩子压入队列时,对应的均为空,则可以再次循环,取队首元素,重新对比;二、有一个节点为空时,因为第一种情况,排除了两者都为NULL,所以这里只是其中有一个为NULL,则返回false;三、对应节点值不等,则返回false;然后依次将两树的左右孩子压入队列中继续循环。

值得注意:若是循环体中,将两树的左右孩子压入队列中,加判断左右孩子是否存在,存在则压,否则不压,则,while条件中既不能用||也不能用&&,因为,||时,会对空队列取首元素,&&时,会造成不能而等,如{1,1}和{1,1,1},所以不能压入时加if判断。

当然可以改变代码的写法,这样可以加if判断,见Grandyang的博客。别的遍历方式应该也能达到同样的实现判断两树是否相同。

 /**
* Definition for binary tree
* 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)
{
queue<TreeNode *> pQ;
queue<TreeNode *> qQ; pQ.push(p);
qQ.push(q);
while( !pQ.empty()&& !qQ.empty()) //这里||和&&都可以AC,
{                     
TreeNode *pNode=pQ.front();
TreeNode *qNode=qQ.front();
pQ.pop();
qQ.pop(); //同时不存在
if(pNode==NULL&&qNode==NULL)
continue;
//有一个不存在
if(pNode==NULL||qNode==NULL)
return false; if(pNode->val !=qNode->val)
return false; pQ.push(pNode->left);
pQ.push(pNode->right);
qQ.push(qNode->left);
qQ.push(qNode->right);
}
return true;
}
};

方法二:

递归大法。终止条件上见;递归式,要注意的是,左右子树必须同时一样才行,所以要用&&

/**
* Definition for binary tree
* 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==NULL&&q==NULL) return true;
else if(p==NULL||q==NULL) return false;
else if(p->val !=q->val) return false; return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
};

[Leetcode] Same tree判断是否为相同树的更多相关文章

  1. [LeetCode] Same Tree 判断相同树

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  2. [LeetCode] Symmetric Tree 判断对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

      思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...

  4. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. LeetCode 101. Symmetric Tree 判断对称树 C++

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  7. hdu 1325 判断有向图是否为树

    题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...

  8. 【js】Leetcode每日一题-叶子相似的树

    [js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...

  9. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

随机推荐

  1. js中split()和join()的用法

    Split()方法:把一个字符串分割成字符串数组 如上所示:把字符串a按空格分隔,得3个字符串数组. 在如: var  a=”hao are you”  a.split(“”);  得到[h,a,o, ...

  2. PHP中对字符串的一些操作

    php中判断字符串在另一个字符串中是否存在(strpos): if(strpos('www.baidu.com', 'www') !== false){ // 存在 }else{ // 不存在 } p ...

  3. php GD图片四角圆形处理

    <?php /** * blog:http://www.zhaokeli.com * 处理四角圆图片 * @param string $imgpath 源图片路径 * @param intege ...

  4. symfony 安装使用(一)

    Symfony安装教程网上已经存在很多了,但是这里还是要写一下: 1.symfony 安装有以下几种,对应不同的环境 1.1通过composer 命令安装 composer create-projec ...

  5. Java学习笔记八:Java的流程控制语句之循环语句

    Java的流程控制语句之循环语句 一:Java循环语句之while: 生活中,有些时候为了完成任务,需要重复的进行某些动作.如参加 10000 米长跑,需要绕 400 米的赛道反复的跑 25 圈.在 ...

  6. HyperLedger Fabric 1.4 超级账本组织(5.3)

    超级账本组织分为TSC(技术指导委员会).Governing Board(董事会成员).LF Staffs(工作人员)三个组织,组织架构图如下: TSC:技术指导委会员,主导社区的开发工作,下设多个工 ...

  7. ABAP CDS ON HANA-(12)ODATA Service

    Create a CDS view and we have the view type as ‘BASIC’ view To publish this as oData, add the annota ...

  8. Markdown 基本用法

    声明:引自 http://www.cnblogs.com/hnrainll/p/3514637.html ,感谢!   1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置 ...

  9. Hibernate-ORM:06.Hibernate中三种状态

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客主要叙述Hibernate中的三种状态:临时状态(瞬时状态),持久状态,游离状态 commit和flu ...

  10. CSS实现简易的轮播图

    <html> <head> <meta charset="UTF-8"> <title></title> <sty ...