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. vue点击按钮复制文本框内容

    1.npm进行安装 npm install clipboard --save 2.在需要使用的组件中import 引用方法:import Clipboard from 'clipboard'; 3.添 ...

  2. ubuntu修复找不到sudo命令

    1.首先,您需要安装该sudo命令.你可以使用 apt 包管理器来做到这一点.您需要以有权安装软件包的用户身份运行此命令,例如root: apt-get install sudo 2.下一步是为您自己 ...

  3. Android实现秒开效果

    0x01 创建SplashActivity 新建一个Activity,取名为SplashActivity 0x02 新建资源 在res/drawable下新建一个splash.xml文件和名为ig_s ...

  4. LC-26

    class Solution { public int removeDuplicates(int[] nums) { int slowIndex = 0, fastIndex = 1; if (num ...

  5. Git原理学习记录

    Git原理学习记录 1.git init git-test ​ git init 实际上就是在特定的目录下创建对应的目录和文件 2.object $ echo "V1" > ...

  6. [个人配置] VSCode Better Comments 扩展配置、高亮注释插件

    在VSCode IDE中,我的代码注释一般都有高亮颜色,那要怎么安装这个插件呢?

  7. Python 报错 ValueError list.remove(x) x not in list 解决办法

    平时开发 Python 代码过程中,经常会遇到这个报错: ValueError: list.remove(x): x not in list 错误提示信息也很明确,就是移除的元素不在列表之中. 比如: ...

  8. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  9. 大一/初学者学C语言前必看!!!(建议收藏)

    目录 数据类型 常量.变量 数组 字符串.转义字符 选择语句 循环语句 函数 操作符 结构体 指针 神秘的学习资料基地​jq.qq.com/?_wv=1027&k=5kWJsY1z 一.数据类 ...

  10. ASP.NET视图视图表单验证

    视图表单验证 初始化项目 新建一个ASP.NET MVC的项目 新建游戏用户类: public class StemUsers { public int id { get; set; } public ...