当前位置:网站首页>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;
}
};
边栏推荐
- Basic use and actual combat sharing of crash tool
- openCV+dlib實現給蒙娜麗莎換臉
- CV learning notes - scale invariant feature transformation (SIFT)
- LeetCode - 919. 完全二叉树插入器 (数组)
- Opencv notes 17 template matching
- Opencv feature extraction - hog
- 4.1 Temporal Differential of one step
- Serial port programming
- CV learning notes - image filter
- Replace the files under the folder with sed
猜你喜欢

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way

Crash工具基本使用及实战分享

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

LeetCode - 933 最近的请求次数

Interruption system of 51 single chip microcomputer

Opencv feature extraction sift

LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)

Opencv+dlib to change the face of Mona Lisa

LeetCode - 715. Range 模块(TreeSet) *****

2.2 DP: Value Iteration & Gambler‘s Problem
随机推荐
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Leetcode 300 最长上升子序列
CV learning notes - edge extraction
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
. DLL and Differences between lib files
03 fastjason solves circular references
Wireshark use
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
Opencv gray histogram, histogram specification
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
2. Elment UI date selector formatting problem
Liquid crystal display
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
2.1 Dynamic programming and case study: Jack‘s car rental
is_ power_ of_ 2 judge whether it is a multiple of 2
(2) New methods in the interface
20220602数学:Excel表列序号
Problems encountered when MySQL saves CSV files
01 business structure of imitation station B project
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer