当前位置:网站首页>LeetCode 每日一题——513. 找树左下角的值
LeetCode 每日一题——513. 找树左下角的值
2022-06-22 20:11:00 【SK_Jaco】
1.题目描述
给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
示例 1:
输入: root = [2,1,3]
输出: 1
示例 2:
输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7
2.解题思路与代码
2.1 解题思路
这道题要获取最左侧的节点值,其实就是在获取最后一层最左边第一个节点,因此考虑使用二叉树层序遍历解答。这里就需要使用到层序遍历的一个特点:放入队列的次数等于队列长度时,那么下一层已全部放入队列中。利用这一特性当放入次数等于队列长度时,此时从队列获取队头即是这一层的第一个节点,遍历完成之后便能得到最后一层第一个节点。这里使用数组来模拟一个队列。
2.2 代码
class Solution {
public int findBottomLeftValue(TreeNode root) {
int head = 0;
int tail = 0;
// 使用数组模拟队列
TreeNode[] queue = new TreeNode[10000];
queue[tail++] = root;
int count = 1;
TreeNode ans = null;
while (tail - head > 0) {
if (count == tail - head) {
// 如果入队数等于队列中元素个数,则 ans 等于对头元素,并且计数清零
ans = queue[head];
count = 0;
}
// 弹出队头元素
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 测试结果
通过测试

3.总结
- 使用二叉树层序遍历,获取每一层第一个节点
- 遍历完成之后便能得到最后一层最左边的节点
- 这里使用数组模拟队列只是单纯的想练习一下队列,也可以用 LinkedList 来操作
边栏推荐
- [redis] three new data types
- 第032讲:异常处理:你不可能总是对的 | 课后测试题及答案
- [book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!
- [records of different objects required by QIPA]
- [redis]redis6 master-slave replication
- 大势智慧创建倾斜模型和切割单体化
- Redis usage scenario sharing (project practice)
- Japanese anime writers and some of their works
- (DUC/DDC)数字上混频/正交下混频原理及matlab仿真
- 2022 question bank and simulated examination for work license of main principals of hazardous chemical business units
猜你喜欢

第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案

第029讲:文件:一个任务 | 课后测试题及答案
![Jerry's problem of opening the near end of four channel call [chapter]](/img/54/d74a90e37deb2d3929f019d695f9ee.png)
Jerry's problem of opening the near end of four channel call [chapter]
![[513. find the value in the lower left corner of the tree]](/img/6d/b2ec8e3072a65c20c586941e6b2a85.png)
[513. find the value in the lower left corner of the tree]

杰理之硬件上 DACL 输出,DAC 输出左右声道的声音【篇】
![[redis] profile](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis] profile

2022年山东省安全员C证考试试题模拟考试平台操作

Cannot re-register id: PommeFFACompetition-v0问题解决

2022年A特种设备相关管理(电梯)考题及模拟考试
![DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]](/img/8a/ce164a5538bd8edf10eba5e4e8abe6.png)
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
随机推荐
Jerry's music mode obtains the directory of playing files [chapter]
第031讲:永久存储:腌制一缸美味的泡菜 | 课后测试题及答案
安卓kotlin sp dp转px
杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
2022年山东省安全员C证考试试题模拟考试平台操作
Correspondence between int and char in C language
杰理之使用 DP 和 DM 做 IO 按键检测注意点【篇】
≥server2012R2系统,禁用系统自带的部分计划任务
Learning websites that programmers must see
Apple corefoundation source code
71- analysis of an Oracle DBA interview with Alibaba in 2010
86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
[redis]配置文件
第026讲:字典:当索引不好用时2 | 课后测试题及答案
300. 最长递增子序列 ●●
2022年A特种设备相关管理(电梯)考题及模拟考试
MySQL adds (appends) prefix and suffix to a column field
[142. circular linked list II]
[redis] cluster and common errors
90- review of several recently optimized Oracle databases