当前位置:网站首页>Leetcode-100:相同的树
Leetcode-100:相同的树
2022-07-03 09:22:00 【ITSOK_U】
100. 相同的树
题目描述:给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
深度优先递归解决(O(min(m,n)),O(min(m,n)))
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!= nullptr && q==nullptr) return false;
else if (p->val != q->val) return false;
else {
// 递————>判断左右子树是否相同
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
}
};
广度优先(O(min(m,n)),O(min(m,n)))
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*> que1,que2;
// 根节点入队
que1.push(p);
que2.push(q);
// 进行层次遍历,注意这里因为是两个树之间的比较
// 所以就不像以前的层次遍历
while(!que1.empty() && !que2.empty()){
// 每次获取队首元素并判断
TreeNode* node1 = que1.front();
que1.pop();
TreeNode* node2 = que2.front();
que2.pop();
// 节点值不相等说明树不相同
if(node1->val!=node2->val) return false;
// 取两个树当前两个队首节点的四个孩子
auto cur1 = node1->left,cur2 = node1->right,cur3 = node2->left,cur4 = node2->right;
// 判断两个节点的左右孩子指针只有其中一个为空则说明树不相同
if(cur1==nullptr ^ cur3==nullptr) return false;
if(cur2==nullptr ^ cur4==nullptr) return false;
// 不为空的节点入队
if(cur1!=nullptr) que1.push(cur1);
if(cur2!=nullptr) que1.push(cur2);
if(cur3!=nullptr) que2.push(cur3);
if(cur4!=nullptr) que2.push(cur4);
}
// 如果层次遍历退出时两个队都已经处理完则说明树相同
return que1.empty() && que2.empty();
}
};
边栏推荐
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
- Opencv histogram equalization
- 2.1 Dynamic programming and case study: Jack‘s car rental
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
- QT self drawing button with bubbles
- After clicking the Save button, you can only click it once
- YOLO_ V1 summary
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- Pycharm cannot import custom package
猜你喜欢

2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)

Yocto technology sharing phase IV: customize and add software package support

Opencv feature extraction - hog

LeetCode - 705 设计哈希集合(设计)

Not many people can finally bring their interests to college graduation

CV learning notes ransca & image similarity comparison hash

Octave instructions

Interruption system of 51 single chip microcomputer

One click generate traffic password (exaggerated advertisement title)

CV learning notes - clustering
随机推荐
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
LeetCode - 673. Number of longest increasing subsequences
Design of charging pile mqtt transplantation based on 4G EC20 module
Installation and removal of MySQL under Windows
Adaptiveavgpool1d internal implementation
LeetCode - 919. 完全二叉树插入器 (数组)
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
Simulate mouse click
Cases of OpenCV image enhancement
CV learning notes - reasoning and training
CV learning notes - deep learning
My notes on intelligent charging pile development (II): overview of system hardware circuit design
RESNET code details
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Yocto Technology Sharing Phase 4: Custom add package support
LeetCode - 715. Range 模块(TreeSet) *****
Leetcode 300 longest ascending subsequence
Pymssql controls SQL for Chinese queries
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
CV learning notes ransca & image similarity comparison hash