当前位置:网站首页>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;
}
};
边栏推荐
- ADS simulation design of class AB RF power amplifier
- LeetCode - 933 最近的请求次数
- Application of external interrupts
- Problems encountered when MySQL saves CSV files
- openCV+dlib实现给蒙娜丽莎换脸
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- 20220607其他:两整数之和
- 4G module initialization of charge point design
- On the problem of reference assignment to reference
- 4G module at command communication package interface designed by charging pile
猜你喜欢

CV learning notes - edge extraction

Connect Alibaba cloud servers in the form of key pairs

03 fastjason solves circular references

Leetcode bit operation

51 MCU tmod and timer configuration

ADS simulation design of class AB RF power amplifier

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

El table X-axis direction (horizontal) scroll bar slides to the right by default

2.2 DP: Value Iteration & Gambler‘s Problem

CV learning notes - camera model (Euclidean transformation and affine transformation)
随机推荐
Opencv image rotation
Application of external interrupts
pycharm 无法引入自定义包
4G module board level control interface designed by charging pile
One click generate traffic password (exaggerated advertisement title)
Tensorflow built-in evaluation
Installation and removal of MySQL under Windows
Wireshark use
使用sed替换文件夹下文件
4G module IMEI of charging pile design
LeetCode - 673. 最长递增子序列的个数
Application of 51 single chip microcomputer timer
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
is_ power_ of_ 2 judge whether it is a multiple of 2
The underlying principle of vector
RESNET code details
Screen display of charging pile design -- led driver ta6932
Swing transformer details-2
Adaptiveavgpool1d internal implementation