513. Find Bottom Left Tree Value - LeetCode
Question
513. Find Bottom Left Tree Value

Solution
题目大意:
给一个二叉树,求最底层,最左侧节点的值
思路:
按层遍历二叉树,每一层第一个被访问的节点就是该层最左侧的节点

Java实现:
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.offer(root); // 向队列追加元素,如果队列满返回false
int left = -1;
while (!nodeQueue.isEmpty()) {
int curLayerSize = nodeQueue.size();
for (int i = 0; i < curLayerSize; i++) {
TreeNode cur = nodeQueue.poll(); // 移除并返回队列头部元素,队列为空返回 null
if (i == 0) left = cur.val; // 当前层的第一个节点最左结点就是最左侧节点
if (cur.left != null) nodeQueue.offer(cur.left);
if (cur.right != null) nodeQueue.offer(cur.right);
}
}
return left;
}
513. Find Bottom Left Tree Value - LeetCode的更多相关文章
- LN : leetcode 513 Find Bottom Left Tree Value
lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...
- 【leetcode】513.Find Bottom Left Tree Value
原题 Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / 1 ...
- 513 Find Bottom Left Tree Value 找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...
- [LC] 513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- 513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)
513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...
- Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)
Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
随机推荐
- Rust 中的数据布局--可选的数据布局
Rust 允许你指定不同于默认的数据布局策略,并为你提供了不安全代码指南. repr(C) 这是最重要的"repr".它的意图相当简单:做 C 所做的事.字段的顺序.大小和对齐方式 ...
- mpvue小程序加载不出图片 Failed to load local image resource
我的GitHub博客,很多内容可以看,喜欢的给星星哦 https://github.com/liangfengbo/frontend 第一道:图片引入本地静态文件失效? mpvue开发小程序时候,要添 ...
- 深入解析丨母婴App如何迅速收割2W新用户?
在讲案例前,我们需要先说一下精细化分析. 我们常说的精细化分析,就是一个持续"解构"的过程,通过像漏斗.留存.细分等高级分析功能,将"整体"按照事件属性解构成& ...
- 前端实现导出excel
结果: 将网页上拿到的数据导出成excel文件 实现: HTML代码 <div> <button type="button" onclick="expo ...
- 关于allegro找不到env文件解决方法
使用allegro的友人时对于env文件并不陌生.在我们设计的过程中经常使用env文件设置快捷键从而达到快速拉线的目的.但是新安装的allegro软件中会找不到env文件,因为今天自己碰到了这件事,并 ...
- linux系统从pci.ids文件获取硬件设备详细厂商信息
机器采样: [root@ht24 hwdata]# cat /etc/redhat-release ; uname -r CentOS Linux release 7.9.2009 (Core) 3. ...
- JVM诊断及工具笔记(4) 使用visualvm分析JVM堆内存泄漏
在这里感谢最近一直阅读我文章的小伙伴,如果觉得文章对你有用,可以帮忙关注转载,需要的时候可以及时找到文章. 背景 今年Q3季度我们在推广业务方使用Iceberg,当时为了让不同业务线的用户可以使用自己 ...
- Git原理学习记录
Git原理学习记录 1.git init git-test git init 实际上就是在特定的目录下创建对应的目录和文件 2.object $ echo "V1" > ...
- 基于kubernetes的分布式限流
做为一个数据上报系统,随着接入量越来越大,由于 API 接口无法控制调用方的行为,因此当遇到瞬时请求量激增时,会导致接口占用过多服务器资源,使得其他请求响应速度降低或是超时,更有甚者可能导致服务器宕机 ...
- Elasticsearch上手指南
目录 ElasticStack及Elasticsearch介绍 Elasticsearch安装 Elasticsearch入门 Elasticsearch配置 Elasticsearch REST A ...