当前位置:网站首页>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();
}
};
边栏推荐
- 2021-11-11 standard thread library
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- (2) New methods in the interface
- Pymssql controls SQL for Chinese queries
- The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
- Basic use and actual combat sharing of crash tool
- CV learning notes ransca & image similarity comparison hash
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- Application of 51 single chip microcomputer timer
猜你喜欢
Notes on C language learning of migrant workers majoring in electronic information engineering
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode - 919. Full binary tree inserter (array)
Development of intelligent charging pile (I): overview of the overall design of the system
03 fastjason solves circular references
Adaptiveavgpool1d internal implementation
LeetCode - 673. Number of longest increasing subsequences
QT is a method of batch modifying the style of a certain type of control after naming the control
Opencv image rotation
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
随机推荐
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
4G module IMEI of charging pile design
Opencv note 21 frequency domain filtering
Yocto technology sharing phase IV: customize and add software package support
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
Vscode markdown export PDF error
Development of intelligent charging pile (I): overview of the overall design of the system
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
03 FastJson 解决循环引用
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Qcombox style settings
2.Elment Ui 日期选择器 格式化问题
On the problem of reference assignment to reference
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
Application of 51 single chip microcomputer timer
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))