当前位置:网站首页>Leetcode-100: same tree
Leetcode-100: same tree
2022-07-03 10:10:00 【ITSOK_ U】
100. Same tree
Title Description : Here are the root nodes of two binary trees p and q , Write a function to check whether the two trees are the same .
If two trees are the same in structure , And the nodes have the same value , They are the same .
Depth first recursive solution (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 {
// Deliver ————> Judge whether the left and right subtrees are the same
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
}
};
breadth-first (O(min(m,n)),O(min(m,n)))
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
// First, judge whether the root node meets the conditions
if(p == nullptr && q==nullptr) return true;
else if(p==nullptr || q==nullptr) return false;
// Initialize two breadth first traversal queues
queue<TreeNode*> que1,que2;
// Root in line
que1.push(p);
que2.push(q);
// Perform hierarchical traversal , Notice here because it's a comparison between two trees
// So it's not like the previous level traversal
while(!que1.empty() && !que2.empty()){
// Get the team leader element every time and judge
TreeNode* node1 = que1.front();
que1.pop();
TreeNode* node2 = que2.front();
que2.pop();
// Unequal node values indicate different trees
if(node1->val!=node2->val) return false;
// Take the four children of the current two team head nodes of the two trees
auto cur1 = node1->left,cur2 = node1->right,cur3 = node2->left,cur4 = node2->right;
// If only one of the left and right child pointers of the two nodes is empty, it indicates that the tree is different
if(cur1==nullptr ^ cur3==nullptr) return false;
if(cur2==nullptr ^ cur4==nullptr) return false;
// Nodes that are not empty are queued
if(cur1!=nullptr) que1.push(cur1);
if(cur2!=nullptr) que1.push(cur2);
if(cur3!=nullptr) que2.push(cur3);
if(cur4!=nullptr) que2.push(cur4);
}
// If both teams have finished processing when the hierarchical traversal exits, the tree is the same
return que1.empty() && que2.empty();
}
};
边栏推荐
- The underlying principle of vector
- Opencv image rotation
- 4G module at command communication package interface designed by charging pile
- 01 business structure of imitation station B project
- QT is a method of batch modifying the style of a certain type of control after naming the control
- Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
- 4G module designed by charging pile obtains signal strength and quality
- Adaptiveavgpool1d internal implementation
- CV learning notes - clustering
- Screen display of charging pile design -- led driver ta6932
猜你喜欢
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
QT self drawing button with bubbles
LeetCode - 715. Range 模块(TreeSet) *****
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
QT is a method of batch modifying the style of a certain type of control after naming the control
LeetCode - 919. Full binary tree inserter (array)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
RESNET code details
2021-10-28
随机推荐
Swing transformer details-2
Opencv note 21 frequency domain filtering
Drive and control program of Dianchuan charging board for charging pile design
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Application of external interrupts
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
Opencv image rotation
20220609其他:多数元素
Replace the files under the folder with sed
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
20220531数学:快乐数
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Tensorflow2.0 save model
Window maximum and minimum settings
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
El table X-axis direction (horizontal) scroll bar slides to the right by default
Stm32 NVIC interrupt priority management