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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...

  4. 【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 ...

  5. 513 Find Bottom Left Tree Value 找树左下角的值

    给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...

  6. [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 ...

  7. 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 ...

  8. LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)

    513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...

  9. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...

随机推荐

  1. 攻防世界 Ditf misc

    Ditf 附件下载下来就是一张图片,我们拉到hxd中发现应该有隐藏的压缩包 我们拉入到kali里面分析 意外发现图片高度被修改过我们先用binwalk分析图片看看 我们先尝试分离一下分离出一个压缩包但 ...

  2. vue H5 超简单的swiper制作抖音上拉切换视频播放

    -----html部分------ <swiper vertical :style="{height: windowheight+'px',width:375+'px'}" ...

  3. APICloud案例源码、模块源码、考试源码、开发工具大集合!赶快收藏

    APICloud专注于APP开发定制技术,多年来不停为开发者奉献更多的资源.此次,APICloud将以往的的资源进行更新.整合,以合集的形式分享给广大的用户. APICloud应用案例源码合集 API ...

  4. 有关表单autocomplete = "off" 失效问题解决方案

    一.autocomplete介绍 autocomplete是Html5中的新属性.该属性规定输入字段是否应该启用自动完成功能.自动完成允许浏览器预测对字段的输入.当用户在字段开始键入的时候,浏览器基于 ...

  5. 集合框架基础三——Map

    Map接口  * 将键映射到值的对象  * 一个映射不能包含重复的键  * 每个键最多只能映射到一个值 Map接口和Collection接口的不同 * Map是双列的,Collection是单列的 * ...

  6. 实现一个promise.all方法

    思路: 1:首先明白all的用法 2:promise.all可以接受一个由promise数组作为参数,并且返回一个promise实例, 3:promise.all([a,b,c...]).then方法 ...

  7. Springcloud报错:java.lang.IllegalStateException: Service id not legal hostname (/a-service)

    今天在做springcloud链路追踪的时候,报错java.lang.IllegalStateException: Service id not legal hostname (/a-service) ...

  8. Oracle数据库包括两个部分数据库和数据库实例

    olsnodes,这个命令用来显示集群点列表(grid即oracle rac的第三个安装包内的软件,可找到) //集群名称[grid@shdb02 ~]$ olsnodes -cshfpdb-clus ...

  9. Java学习——数组的基础知识

    数组的特点.分类:一维.二维数组的使用:数组的声明和初始化.调用数组的指定位置的元素.获取数组的长度.遍历数组.数组元素的默认初始化值

  10. 硬核 | Redis 布隆(Bloom Filter)过滤器原理与实战

    在Redis 缓存击穿(失效).缓存穿透.缓存雪崩怎么解决?中我们说到可以使用布隆过滤器避免「缓存穿透」. 码哥,布隆过滤器还能在哪些场景使用呀? 比如我们使用「码哥跳动」开发的「明日头条」APP 看 ...