题面

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

思路

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. ios排序NSArray(数字.字符串)

    NSArray *originalArray = @[@"1",@"21",@"12",@"11",@"0&q ...

  2. 123457123456#0#-----com.twoapp.TruckCarRun01--前拼后广--大卡车游戏jiemei

    com.twoapp.TruckCarRun01--前拼后广--大卡车游戏jiemei

  3. UIwindow ---密码框

    程序运行显示结果如下 : 验证密码输入错误显示如下: 代码如下 : 1> ////  PasswordInputWindow.m//  UIWindow--密码框////  Created by ...

  4. expect自动登录

    .安装软件 yum install expect -y .查看expect命令位置 expect命令的位置也可能是在 /usr/local/bin/expect,使用前用户应确认工具是否存在(确认方法 ...

  5. redis列表和有序集合

    redis中的list数据类型是可以插入重复数据的,有去重的需求的话可以用redis有序集合数据类型 Redis Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中. 如果某个成员已经是 ...

  6. Matlab给曲线添加加参考线

    声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 对于Matlab的使用情况常常是这样子的,很多零碎的函数名字很难记忆,经常用过后过一段时间就又忘记了,又得去网 ...

  7. 最新 三六零java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.三六零等10家互联网公司的校招Offer,因为某些自身原因最终选择了三六零.6.7月主要是做系统复习.项目复盘.LeetCo ...

  8. java中创建线程的方式

    创建线程的方式: 继承thread 实现runnable 线程池 FurureTask/Callable 第一种:继承thread demo1: public class demo1 { public ...

  9. 【leetcode算法-中等】2. 两数相加

    [题目描述] 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表 ...

  10. fiddler手机抓包1

    1.手机抓包配置教程:https://www.jianshu.com/p/724097741bdf 2.