当前位置:网站首页>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;
}
};
边栏推荐
- MySQL root user needs sudo login
- Google browser plug-in recommendation
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- Pycharm cannot import custom package
- 使用密钥对的形式连接阿里云服务器
- LeetCode - 5 最长回文子串
- LeetCode - 919. 完全二叉树插入器 (数组)
- LeetCode - 900. RLE 迭代器
- Retinaface: single stage dense face localization in the wild
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
猜你喜欢

Opencv notes 17 template matching

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

LeetCode - 673. Number of longest increasing subsequences

CV learning notes - deep learning

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

openCV+dlib實現給蒙娜麗莎換臉

Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn

One click generate traffic password (exaggerated advertisement title)

LeetCode - 933 最近的请求次数

Leetcode - 933 number of recent requests
随机推荐
LeetCode - 715. Range 模块(TreeSet) *****
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
20220608其他:逆波兰表达式求值
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
4G module designed by charging pile obtains signal strength and quality
is_ power_ of_ 2 judge whether it is a multiple of 2
Matplotlib drawing
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
Leetcode bit operation
Opencv notes 17 template matching
Sending and interrupt receiving of STM32 serial port
Opencv interview guide
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
CV learning notes - clustering
Vscode markdown export PDF error
4G module board level control interface designed by charging pile
Qcombox style settings
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
QT self drawing button with bubbles