当前位置:网站首页>力扣解法汇总513-找树左下角的值
力扣解法汇总513-找树左下角的值
2022-07-22 08:55:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
示例 1:
输入: root = [2,1,3]
输出: 1
示例 2:
输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7
提示:
二叉树的节点个数的范围是 [1,104]
-231 <= Node.val <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-bottom-left-tree-value
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 用层序便利的方式可以解决,取最底层的list的第一个节点即可
代码:
public class Solution513 {
public int findBottomLeftValue(TreeNode root) {
List<TreeNode> list = new ArrayList<>();
list.add(root);
TreeNode treeNode = levelSearch(list);
return treeNode.val;
}
private TreeNode levelSearch(List<TreeNode> list) {
List<TreeNode> nextList = new ArrayList<>();
for (TreeNode node : list) {
if (node.left != null) {
nextList.add(node.left);
}
if (node.right != null) {
nextList.add(node.right);
}
}
if (nextList.size() == 0) {
return list.get(0);
}
return levelSearch(nextList);
}
}
边栏推荐
- GeoWebCache发布ArcGIS切片数据
- 需要达到什么水平,程序员才能顺利拿到20k月薪无压力?
- 腾讯持久化框架MMKV原理探究
- Gbase8s database comparison performed by database objects
- VCs and Verdi learning records
- Hblock revitalizes the enterprise storage market
- QT笔记——自定义数据类型
- Industrial Internet security situation prediction method integrating attention mechanism and bsru
- 一线互联网P7面试题总结---补充中
- 【Rust】为什么我建议你学一下 Rust | Rust 初探
猜你喜欢
「武汉理工大学 软件工程复习」第一章 | 软件工程概述
GeoWebCache发布ArcGIS切片数据
Top 10 active noise reduction headphones and top 10 active noise reduction headphones brands
【Rust】我该用什么软件开发 Rust | 常用支持 Rust 的编辑器推荐
Fcntl function
Mutexes and semaphores
Fabric.js 居中元素
Reentrant function
C语言中continue的理解(fishing_1)
A trick to teach you how to visualize recruitment data ~ is there anyone who can't analyze these data cases?
随机推荐
Dr. water
力扣解法汇总814-二叉树剪枝
"Review of software engineering in Wuhan University of technology" Chapter 7 | software testing
在ubuntu中使用pypyodbc无法连接sql server
女嘉宾报名
MySQL密碼正確但是啟動報錯Unable to create initial connections of pool.Access denied for user ‘root‘@‘localhost
Fabric.js 居中元素
QT笔记——QtXml
Distsql deep parsing: creating a dynamic distributed database
C语言结构体初始化的四种方法
QT exe is only allowed to run a single
Qt Notes - nombre de lignes traînées et de mouvements pour le Widget qtablewidget
How to build a clear and understandable data Kanban?
基于细粒度嵌入空间预留的密文域图像可逆信息隐藏方法
Shell script uses expect automatic interactive login to remote host for batch shutdown
Gbase8s database minus operator
Female guest registration
Gbase8s database set database object mode statement
C语言中continue的理解(fishing_1)
QT notes - qudpsocket of network communication