当前位置:网站首页>[high frequency written test questions] 513 Find the value in the lower left corner of the tree
[high frequency written test questions] 513 Find the value in the lower left corner of the tree
2022-06-22 12:11:00 【Gong Shui Sanye's Diary】
Title Description
This is a LeetCode Upper 513. Find the value in the lower left corner of the tree , The difficulty is secondary .
Tag : 「BFS」、「DFS」、「 Tree traversal 」
Given a binary tree The root node 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 .
Example 1: 
Input : root = [2,1,3]
Output : 1
Example 2: 
Input : [1,2,3,4,null,5,6,null,null,7]
Output : 7
Tips :
The range of the number of nodes in a binary tree is
BFS
Use BFS Conduct 「 Sequence traversal 」, Update each time with the first node of the current layer ans, When BFS After the end ,ans The storage is the leftmost node of the last layer .
Code :
class Solution {
public int findBottomLeftValue(TreeNode root) {
Deque<TreeNode> d = new ArrayDeque<>();
d.addLast(root);
int ans = 0;
while (!d.isEmpty()) {
int sz = d.size();
ans = d.peek().val;
while (sz-- > 0) {
TreeNode poll = d.pollFirst();
if (poll.left != null) d.addLast(poll.left);
if (poll.right != null) d.addLast(poll.right);
}
}
return ans;
}
}
Time complexity : Spatial complexity : In the worst case, all nodes are on the same layer , The complexity is
DFS
Empathy , have access to DFS Traverse the tree , Priority each time DFS The left subtree of the current node , Search to the current depth for the first time depth when , It must be the leftmost node of the current depth , In this case, the current node value is used to update ans.
Code :
class Solution {
int max, ans;
public int findBottomLeftValue(TreeNode root) {
dfs(root, 1);
return ans;
}
void dfs(TreeNode root, int depth) {
if (root == null) return ;
if (depth > max) {
max = depth; ans = root.val;
}
dfs(root.left, depth + 1);
dfs(root.right, depth + 1);
}
}
Time complexity : Spatial complexity : Degenerate into chain in the worst case , The recursion depth is . The complexity is
Last
This is us. 「 Brush through LeetCode」 The first of the series No.513 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
边栏推荐
- 表格转换为LaTex格式
- 牛客挑战赛53C
- Deadlock found when trying to get lock; try restarting transaction 【MySQL死锁问题解决】
- CF736 D2
- Explanation of 94F question in Niuke practice match
- 《Go题库·10》channel和锁的对比
- Noun analysis: ETL
- Redis - 5. Jedis operation redis6
- 【高频笔试题】513. 找树左下角的值
- Struggle, programmer -- Chapter 41 all kinds of things today are like water without trace; On the eve of the Ming Dynasty, you are a stranger
猜你喜欢

Redis - 7. Opérations de transaction

Markov chain, hidden Markov model

When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
![[2206] An Improved One millisecond Mobile Backbone](/img/75/b040f4b88050937dee57003b62f7b0.png)
[2206] An Improved One millisecond Mobile Backbone

Matlab的KNN分類使用(附源碼),實現像素分類(自己設置訓練集比例),打印測試精度

Redis - 12、应用问题解决

【高频笔试题】513. 找树左下角的值

求100-200之间全部的素数

Redis - 4、新的3种数据类型
![Exchange the nodes in the linked list in pairs [the principle of one-way linked list without chain]](/img/67/8e9f3c396a8f529a616964b69cc47f.png)
Exchange the nodes in the linked list in pairs [the principle of one-way linked list without chain]
随机推荐
Reader case of IO
美团基于 Flink 的实时数仓平台建设新进展
In a word, several common methods of uploading Trojan horse
KNN classification of MATLAB (with source code) is used to realize pixel classification (set the proportion of training set by yourself) and print test accuracy
Noun analysis: ETL
【2022暑期】【LeetCode】31. 下一个排列
TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
CF736 D2
Arc128 C convex hull optimized suffix and?
New progress in the construction of meituan's Flink based real-time data warehouse platform
Foreign lead needs energy, interest, research, diligence and is indispensable
俞敏洪称未来可能开电商学院;马斯克儿子申请断绝父子关系;饿了么回应大量用户收到免单信息;B站上线付费视频...
马尔可夫链(Markov Chain),隐马尔可夫模型
《Go题库·10》channel和锁的对比
Redis - 4. Three new data types
"Dare not doubt the code, but have to doubt the code" a network request timeout analysis
sql注入绕过方法总结
IO之ByteArrayStream案例
Messari年度报告-2021
成功案例 | 安超云助力兰州大学第二医院搭建新型IT基础设施平台 提升医疗信息资源利用率