当前位置:网站首页>Leetcode-513: find the lower left corner value of the tree
Leetcode-513: find the lower left corner value of the tree
2022-07-03 10:10:00 【ITSOK_ U】
513. Find the value in the lower left corner of the tree
Title Description :
Given the root node of a binary tree root, Please find the of the binary tree At the bottom Leftmost The value of the node . Suppose there is at least one node in the binary tree .
What needs to be grasped is in the question Leftmost It means , Because it's the leftmost , Therefore, according to the requirements of the topic, the node we seek is not necessarily the left leaf node , It may also be the deepest right leaf , Here are a few examples and their results
According to the above diagram, we can easily know that the solution goal is The value of the deepest leftmost node of the tree . There are generally two directions for solving this kind of problem : Depth first and breadth first , Generally speaking, for solving problems involving levels , Breadth first has clearer logic . Let's analyze and solve the problem from two perspectives .
Depth first DFS(O(N),O(N))
Depth first is achieved by recursion based on preorder traversal . To facilitate hierarchical comparison, we use a reference parameter curdepth To record The level of the leftmost value that has been calculated Easy update iteration . In addition, use depth To represent the level of the current node , To ensure the correctness of backtracking ( Do not interfere with the deep update of other nodes ),depth It needs to be set as a normal parameter .
class Solution {
public:
// among num Is the value that needs to be recorded ,depth Is the depth of the currently accessed tree node
// curdepth Is the depth of the node corresponding to the saved return value
void getLeft(TreeNode* root, int &num,int depth,int &curdepth){
if(root == nullptr) return;
// Save the leftmost element of each layer
if(root->left == nullptr && root->right == nullptr){
// depth>curdepth It means that the current node is the leftmost node of this layer
if(depth > curdepth){
curdepth = depth;
num = root->val;
}
}
getLeft(root->left,num,depth+1,curdepth);
getLeft(root->right,num,depth+1,curdepth);
}
int findBottomLeftValue(TreeNode* root) {
int n = root->val;
int dep = 0;
if(!root->left && !root->right) return n;
getLeft(root,n,1,dep);
return n;
}
};
breadth-first BFS(O(N),O(1))
It is relatively simple to solve the problem of breadth first , You only need to save it when you enter a new layer for the first time Team leader It is the node value of the leftmost element of this layer .
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
// If only the root node , Return the value of the root node
if(!root ->left && !root->right) return root->val;
// Level traversal
queue<TreeNode*> que;
que.push(root);
// Initialization return value
int res = 0;
// Access breadth traverses the queue
while(!que.empty()){
size_t size = que.size();
TreeNode* node = nullptr;
// Take the leftmost element of each layer
res = que.front()->val;
// Process elements by layer
while(size--){
node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
// Return results
return res;
}
};
边栏推荐
- Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
- Circular queue related design and implementation reference 1
- (1) What is a lambda expression
- Serial communication based on 51 single chip microcomputer
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- Basic use and actual combat sharing of crash tool
- 20220608其他:逆波兰表达式求值
- Opencv Harris corner detection
- Opencv notes 20 PCA
- Dynamic layout management
猜你喜欢
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
LeetCode - 933 最近的请求次数
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
Retinaface: single stage dense face localization in the wild
Pycharm cannot import custom package
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
2. Elment UI date selector formatting problem
Opencv histogram equalization
随机推荐
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
4G module at command communication package interface designed by charging pile
51 MCU tmod and timer configuration
CV learning notes - scale invariant feature transformation (SIFT)
Cases of OpenCV image enhancement
Opencv feature extraction sift
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
01 business structure of imitation station B project
1. Finite Markov Decision Process
03 fastjason solves circular references
Matplotlib drawing
Google browser plug-in recommendation
2021-10-28
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
El table X-axis direction (horizontal) scroll bar slides to the right by default