当前位置:网站首页>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
边栏推荐
- 86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
- [redis]配置文件
- Arcgis中las点云数据抽稀
- 2022年山东省安全员C证考试试题模拟考试平台操作
- 第021讲:函数:lambda表达式 | 课后测试题及答案
- Redis核心技术与实战:学习总结目录
- 70 root cause analysis Oracle database sudden performance problems, who will take the blame
- When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]
- Use Charles to capture packets
- Watch,computed和methods的区别
猜你喜欢

Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model

Lesson 019: function: my site listen to my after-school test questions and answers

杰理之开启四声道通话近端卡顿问题【篇】

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

Redis learning notes

redis学习笔记
![[records of different objects required by QIPA]](/img/f7/c0f0f56e4f1bf4f1a0a61552afcd2b.png)
[records of different objects required by QIPA]

鸿蒙第三次培训

Redis usage scenario sharing (project practice)
![kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动](/img/29/8dd188cc4e909562862b5f2c57c898.png)
kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
随机推荐
2022 question bank and simulated examination for work license of main principals of hazardous chemical business units
第031讲:永久存储:腌制一缸美味的泡菜 | 课后测试题及答案
杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】
Simulated 100 questions and simulated examination of hoisting machinery command examination in 2022
es 按条件查询数据总条数
79- do not create desc descending index when you see order by XXX desc - there is book donation benefit at the end of the article
IDC发布中国数据治理报告 亿信华辰第一
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
Cannot re-register id: PommeFFACompetition-v0问题解决
ACM. Hj24 chorus ●●
Android kotlin SP DP to PX
88- widely circulated parameter optimization, honey or poison?
Redis usage scenario sharing (project practice)
杰理之使用 DP 和 DM 做 IO 按键检测注意点【篇】
2022危险化学品经营单位主要负责人上岗证题库及模拟考试
第029讲:文件:一个任务 | 课后测试题及答案
鸿蒙第三次培训
[redis] three new data types
Baijia forum Daqin rise (lower part)
[redis] publish and subscribe