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. pyqt4学习资料

    官方文档: http://pyqt.sourceforge.net/Docs/PyQt4/classes.html 啄木鸟社区:https://wiki.woodpecker.org.cn/moin/ ...

  2. MySQL数据库查看数据表占用空间大小和记录数

    MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHE ...

  3. hdcms v5.7.0学习笔记

    hdcms v5.7.0学习笔记 https://note.youdao.com/ynoteshare1/index.html?id=c404d63ac910eb15a440452f73d6a6db& ...

  4. C语言结构体的学习,以及gdb的调式

    #include <stdio.h> #include <string.h> #define format "%d\n%s\n%f\n%f\n%f\n" t ...

  5. [Cracking the Coding Interview] 4.4 Check Balanced

    Implement a function to check if a binary tree is balanced. For the purpose of this question, a bala ...

  6. PHP.44-TP框架商城应用实例-后台19-权限管理-RBAC需求分析

    RBAC:Role Based Access Control:基于角色的访问控制 需求分析:[类似效果如下图] 1.权限,角色,管理员 2.权限管理[无限级] 注意:权限会被分配给角色,而不是给管理员 ...

  7. 521. [NOIP2010] 引水入城 cogs

    521. [NOIP2010] 引水入城 ★★★   输入文件:flow.in   输出文件:flow.out   简单对比时间限制:1 s   内存限制:128 MB 在一个遥远的国度,一侧是风景秀 ...

  8. python基础——元组、文件及其它

    Python核心数据类型--元组 元组对象(tuple)是序列,它具有不可改变性,和字符串类似.从语法上讲,它们便在圆括号中,它们支持任意类型.任意嵌套及常见的序列操作. 任意对象的有序集合:与字符串 ...

  9. Vuex实践

    本文来自网易云社区 作者:刘凌阳 前言 2017年对于Vue注定是不平凡的一年.凭借着自身简介.轻量.快速等特点,Vue俨然成为最火的前端MVVM开发框架.随着Vue2.0的release,越来越多的 ...

  10. 纯原生仿ES6的Object.assign,实现深度合并对象

    源码: function isObj(x){ var type = typeof x; return x !== null && (type === 'object' || type ...