leetcode-100. Same Tree · Tree + DFS + Queue
题面
对比两棵二叉树是否相同,返回结果。
思路
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的更多相关文章
- LeetCode(100)题解--Same Tree
https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if th ...
- [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 ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)
Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...
- Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)
Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- [LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode 971. Flip Binary Tree To Match Preorder Traversal
原题链接在这里:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 题目: Given a bina ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_12-前端集成认证授权-携带JWT授权
携带JWT授权 登陆后jwt已经存到了sessionStorage里面 当进行微服务的请求就要携带令牌.我们在拦截器里面做文章. 在axios的intercepters拦截器上 手工的给这个课程 造一 ...
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_03-方法授权-jwt令牌包含权限
修改认证服务的UserDetailServiceImpl类,下边的代码中 permissionList列表中存放了用户的权限, 并且将权限标识按照中间使用逗号分隔的语法组成一个字符串,最终提供给Spr ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_12-SpringSecurityOauth2研究-JWT研究-生成私钥和公钥
3.6.3 JWT入门 Spring Security 提供对JWT的支持,本节我们使用Spring Security 提供的JwtHelper来创建JWT令牌,校验JWT令牌 等操作. 3.6.3. ...
- Mybase解决保存文件后再打开不能修改编辑
1.问题复现 2.解决方式 3.可以修改编辑
- SSRS 2016 Forms Authentication
SSRS 2016 comes with completely new report manager web interface and implementing form authenticatio ...
- Win10利用CodeBlocks搭建Objective-C开发环境(二)
工程文件已经建好:但此时会发现main.m文件为灰色,且无法点击,此时需右键点击main.m文件,在option选项中勾选 compile file和 link file选项. 设置完成后,双击mai ...
- Python(1)自动发送邮件
python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email模块主要负责构造邮件. sm ...
- blender-编译源码
1. 获得源码,目前是以 2.8 版本为例子 https://www.blender.org/download/ 上,可以直接下面源码 2. 解压,在blender-2.80 目录下,运行 mak ...
- mybatis 找不到映射器xml文件 (idea)
原因是: idea不会编译src的java目录的xml文件 所以解决思路就是:将IDEA maven项目中src源代码下的xml等资源文件编译进classes文件夹 具体操作方法就是:配置maven的 ...
- Selenium问题集锦
此文章用于记录使用Selenium遇见的问题~ 问题1:sendkeys直接报错如下: 解决方案:selenium 驱动和Chrome浏览器的版本必须对应,不然会报此错.驱动地址:点此跳转 下载前先看 ...