当前位置:网站首页>Leetcode daily question - 513 Find the value in the lower left corner of the tree
Leetcode daily question - 513 Find the value in the lower left corner of the tree
2022-06-22 21:39:00 【SK_ Jaco】
1. Title Description
513. Find the value in the lower left corner of the tree
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
2. Problem solving ideas and codes
2.1 Their thinking
This problem is to get the leftmost node value , In fact, it is the first node on the left of the last layer , So consider using binary tree sequence traversal solution . Here we need to use a feature of sequence traversal : When the number of times put in the queue is equal to the length of the queue , Then the next level has been put into the queue . Using this feature, when the number of places is equal to the length of the queue , At this point, the first node of this layer is to obtain the queue header from the queue , After traversal, you can get the first node of the last layer . Here, an array is used to simulate a queue .
2.2 Code
class Solution {
public int findBottomLeftValue(TreeNode root) {
int head = 0;
int tail = 0;
// Use arrays to simulate queues
TreeNode[] queue = new TreeNode[10000];
queue[tail++] = root;
int count = 1;
TreeNode ans = null;
while (tail - head > 0) {
if (count == tail - head) {
// If the number of entries equals the number of elements in the queue , be ans Equal to header element , And the count is cleared
ans = queue[head];
count = 0;
}
// Pop team leader element
TreeNode poll = queue[head++];
if (poll.left != null) {
queue[tail++] = poll.left;
count++;
}
if (poll.right != null) {
queue[tail++] = poll.right;
count++;
}
}
return ans.val;
}
}
2.3 test result
Pass the test

3. summary
- Use binary tree sequence traversal , Get the first node of each layer
- After traversal, you can get the leftmost node of the last layer
- Here, we use arrays to simulate queues. We just want to practice queues , It can also be used. LinkedList To operate
边栏推荐
- 解决phpstudy中mysql无法启动,与本地安装的mysql冲突
- 万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
- kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
- 嵌入式开发基础之任务管理(线程管理)
- Cannot re-register id: PommeFFACompetition-v0问题解决
- Android kotlin SP DP to PX
- 92 match for several_ Recognize SQL write example
- Jericho uses DP and DM for IO key detection [chapter]
- 查询es分页下标超过1万
- Install MySQL in ECS (version 2022)
猜你喜欢

CVPR2022 | 海德堡大学《深度视觉相似性与度量学习》教程

第019讲:函数:我的地盘听我的 | 课后测试题及答案

Install MySQL in ECS (version 2022)
![[redis]redis6 transaction operations](/img/50/639867a2fcb92082ea262a8a19bb68.png)
[redis]redis6 transaction operations
![When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]](/img/0a/93e95a3a2a923497d57895508ce92e.png)
When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]

ICML2022 | 利用虚拟节点促进图结构学习

Redis learning notes

长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!

2022 a special equipment related management (elevator) examination questions and simulation examination

Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers
随机推荐
ACM. HJ24 合唱队 ●●
TC397 Flash
第032讲:异常处理:你不可能总是对的 | 课后测试题及答案
[redis]redis persistence
第023、024讲:递归:这帮小兔崽子、汉诺塔 | 课后测试题及答案
第026讲:字典:当索引不好用时2 | 课后测试题及答案
第016讲:序列 | 课后测试题及答案
How to operate redis on the IntelliJ idea database console
浅析 Open API 设计规范
《跟唐老师学习云网络》 - OpenStack网络实现
鸿蒙第三次培训
[redis]redis6 master-slave replication
Operation of simulation test platform for 2022 Shandong safety officer C certificate test
第022讲:函数:递归是神马 | 课后测试题及答案
Fluent system architecture
86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
Apple GCD source code
杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
牛客 52次月赛 C 说谎的机器 (区间赋值操作由O(n^2)转为O(n)的复杂度)
92 match for several_ Recognize SQL write example