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) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
随机推荐
- Easyx库安装教程
目录: 安装 使用 帮助文档 安装 打开Easyx官网https://easyx.cn/ 点击图中下载按钮,下载Easyx库.或者直接点此下载 双击运行 图中标注的绿色框内为官方提供的帮助文档,红色框 ...
- Numpy非常重要有用的数组合并操作
Numpy非常重要有用的数组合并操作 背景:在给机器学习准备数据的过程中,经常需要进行不同来源的数据合并的操作. 两类场景: 给已有的数据添加多行,比如增添一些样本数据进去: 给已有的数据添加多列,比 ...
- HTML5打造原生应用——Ionic框架简介与Ionic Hello World
试了试用Ionic框架打造了两个应用,然后在Google Play上架了. 程序语言答人 教你设计物联网 更有意思的是这是在一周的业余时间内完成的三个应用中的两个,接着让我们看看这个框架如何实现高效地 ...
- python-你好
你的程序会读入一个名字,比如John,然后输出"Hello John". 输入格式: 一行文字. 输出格式: 一行文字. 输入样例: Mary Johnson 输出样例: Hell ...
- springboot插件
目前spring官网(http://spring.io/tools/sts/all)上可下载的spring插件只有:springsource-tool-suite-3.8.4(sts-3.8.4).但 ...
- IO流入门
@ 目录 总结内容 1. IO流是什么 2. 字符流和字节流 3. File常用API(前面类型为返回类型) 4. 编码转换 5. IO流实现流程 6. 输入输出流简单实现 7. 输入输出流简单实现 ...
- 彻底弄懂小程序e.target与e.currentTarget
一.小程序中关于事件对象 e 的属性中有两个特别重要的属性:target与currentTarget属性:对于这两个属性,官方文档上的解释是: target:事件源组件对象 currentTarg ...
- 理解Promise函数中的resolve和reject
看了promise的用法,一直不明白里面的resolve和reject的用法: 运行了这两段代码之后彻底理解了promise的用法: var p = new Promise(function (res ...
- jupyter notebook 调用.py文件
方法1.利用 %run xx.py 直接运行得出结果. 方法2:利用 %load xx.py 载入代码再点击Run运行,这种方法的好处是可以方便修改代码. 说明: Jupyter Notebook中以 ...
- Spring Boot-@EnableWebMvc注解
作用:当配置类中添加了该注解了之后,就表示某个模块的自动配置就都失效了,全部都要自己配置 例如下面这个MVC模块的配置类 /** * @author:抱着鱼睡觉的喵喵 * @date:2020/12/ ...