当前位置:网站首页>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;
}
};
边栏推荐
- 4G module designed by charging pile obtains signal strength and quality
- LeetCode - 673. 最长递增子序列的个数
- Opencv+dlib to change the face of Mona Lisa
- CV learning notes convolutional neural network
- QT detection card reader analog keyboard input
- 【C 题集】of Ⅵ
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- 20220605数学:两数相除
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- Installation and removal of MySQL under Windows
猜你喜欢
The underlying principle of vector
ADS simulation design of class AB RF power amplifier
2. Elment UI date selector formatting problem
Opencv notes 17 template matching
LeetCode - 715. Range 模块(TreeSet) *****
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
yocto 技術分享第四期:自定義增加軟件包支持
Opencv gray histogram, histogram specification
CV learning notes alexnet
Opencv feature extraction - hog
随机推荐
Opencv note 21 frequency domain filtering
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
2021-11-11 standard thread library
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
It is difficult to quantify the extent to which a single-chip computer can find a job
LeetCode - 919. Full binary tree inserter (array)
Working mode of 80C51 Serial Port
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
1. Finite Markov Decision Process
20220609其他:多数元素
About windows and layout
20220605数学:两数相除
Pymssql controls SQL for Chinese queries
LeetCode - 673. Number of longest increasing subsequences
Tensorflow2.0 save model
Development of intelligent charging pile (I): overview of the overall design of the system
20220607其他:两整数之和
CV learning notes - edge extraction
Yocto Technology Sharing Phase 4: Custom add package support
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)