当前位置:网站首页>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 - 933 最近的请求次数
- CV learning notes alexnet
- 01仿B站项目业务架构
- 2. Elment UI date selector formatting problem
- 4G module IMEI of charging pile design
- Positive and negative sample division and architecture understanding in image classification and target detection
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- Opencv interview guide
- getopt_ Typical use of long function
猜你喜欢
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
LeetCode - 706 设计哈希映射(设计) *
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
Serial communication based on 51 single chip microcomputer
51 MCU tmod and timer configuration
Opencv notes 20 PCA
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
CV learning notes - clustering
Yocto Technology Sharing Phase 4: Custom add package support
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
随机推荐
Basic use and actual combat sharing of crash tool
is_ power_ of_ 2 judge whether it is a multiple of 2
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Problems encountered when MySQL saves CSV files
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
01仿B站项目业务架构
QT self drawing button with bubbles
. DLL and Differences between lib files
LeetCode - 5 最长回文子串
Opencv interview guide
Qcombox style settings
CV learning notes - feature extraction
(2)接口中新增的方法
Toolbutton property settings
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
Blue Bridge Cup for migrant workers majoring in electronic information engineering
03 FastJson 解决循环引用
2021-11-11 standard thread library
MySQL root user needs sudo login
LeetCode - 673. 最长递增子序列的个数