题面

对比两棵二叉树是否相同,返回结果。

思路

1. 递归解决DFS

首先判断根节点,如果都空,返回true;

如果一空一不空,返回false;

如果都不空,判断两节点值是否相同,若不同,返回false,若相同,递归左子树、右子树

源码

 /**
* 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 == nullptr && q == nullptr)
return true;
else if(p == nullptr || q == nullptr)
return false;
else
{
if(p->val == q->val)
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
else
return false;
}
}
};

2. 层序遍历解决: 队列

1. 首先判断根节点,如果都空,返回true;

2. 如果一空一不空,返回false;

3. 如果都不空,新建队列,两节点入队。如果队列不空,出队两个元素,如果都为空,继续continue(而不是像判断头节点那样返回true),如果一空一不空,返回false;

4. 如果都不空,判断该值是否相等,若不相等,返回false; 若相等,那么分别将当前节点左孩子(2个)、右孩子(2个)入队,循环。

即:这种方法是为了在过程中返回false,最后遍历完毕,返回true.

源码

 class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p == nullptr && q == nullptr)
return true;
else if(p == nullptr || q == nullptr)
return false;
queue<TreeNode*> tmp;
tmp.push(p); tmp.push(q);
while(!tmp.empty())
{
TreeNode *pp, *qq;
pp = tmp.front(); tmp.pop();
qq = tmp.front(); tmp.pop();
if(pp == nullptr && qq == nullptr)
continue;
else if(pp == nullptr || qq == nullptr)
return false; if(pp->val != qq->val)
return false;
else
{
tmp.push(pp->left); tmp.push(qq->left);
tmp.push(pp->right); tmp.push(qq->right);
}
}
return true;
}
};

leetcode-100. Same Tree · Tree + DFS + Queue的更多相关文章

  1. LeetCode(100)题解--Same Tree

    https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if th ...

  2. [LeetCode] 100. Same Tree_Easy tag: DFS

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

  3. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  4. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

  5. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...

  6. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  7. [LeetCode] 100. Same Tree 相同树

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

  8. LeetCode 971. Flip Binary Tree To Match Preorder Traversal

    原题链接在这里:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 题目: Given a bina ...

  9. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  10. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

随机推荐

  1. POPUP_GET_VALUES 金额字段不可编辑

    转自:https://blog.csdn.net/huanglin6/article/details/102733845 当在POPUP_GET_VALUES函数中参考的字段是个货币或者金额字段的话, ...

  2. (四)HttpServletRequest对象(转)

    转自“孤傲苍狼”博客. Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即 ...

  3. 小程序scroll-view 使用

    <scroll-view class="box" scroll-x="true" > <view </view> <view ...

  4. Java使用Apache Commons Net的FtpClient进行下载时会宕掉的一种优化方法

    在使用FtpClient进行下载测试的时候,会发现一个问题,就是我如果一直重复下载一批文件,那么经常会宕掉. 也就是说程序一直停在那里一动不动了. 每个人的情况都不一样,我的情况是因为我在本地之前就有 ...

  5. nfs搭建;nfs监控;mount对于nfs的相应配置

    nfs搭建 https://www.cnblogs.com/lms0755/p/9211585.html https://www.jianshu.com/p/e47cccbb3ae5 https:// ...

  6. Flarum 安装问题 编译安装 fileinfo.so

    大部分人上传 会遇到文件没有权限这一问题 ,这个好解决 下边是服务器 php.ini 扩展 fileinfo 由于php 版本是7.1.8 最新的 又是用的一键环境安装 所以 没有安装这个扩展 1.去 ...

  7. Go之接口interface(1)

    1. 什么是interface在此之前,我们遇到的都是具体的类型,比如数字类型.切片类型等等.对于这些具体的类型,我们总是能知道它是什么.可以利用它来做什么,比如对于一个数字类型,我们知道可以对其进行 ...

  8. Linux内核编译、安装流程

    原文链接:https://blog.csdn.net/qq_28437139/article/details/83692907 此处只讲linux内核编译步骤至于安装虚拟机,安装ubuntu操作系统请 ...

  9. EF-初识

    什么是ORM 起源随着编程的发展,程序里都是面向对象啥的,但是数据库发展呢  网状数据库 ->层次数据库 ->关系数据库(当然还有nosql数据库  我们只是做热数据缓存  后面将会讲到) ...

  10. HTTP最常见的响应头

    HTTP最常见的响应头如下所示: l         Allow:服务器支持哪些请求方法(如GET.POST等): l         Content-Encoding:文档的编码(Encode)方法 ...