Question

513. Find Bottom Left Tree Value

Solution

题目大意:

给一个二叉树,求最底层,最左侧节点的值

思路:

按层遍历二叉树,每一层第一个被访问的节点就是该层最左侧的节点

Java实现:

  1. public int findBottomLeftValue(TreeNode root) {
  2. Queue<TreeNode> nodeQueue = new LinkedList<>();
  3. nodeQueue.offer(root); // 向队列追加元素,如果队列满返回false
  4. int left = -1;
  5. while (!nodeQueue.isEmpty()) {
  6. int curLayerSize = nodeQueue.size();
  7. for (int i = 0; i < curLayerSize; i++) {
  8. TreeNode cur = nodeQueue.poll(); // 移除并返回队列头部元素,队列为空返回 null
  9. if (i == 0) left = cur.val; // 当前层的第一个节点最左结点就是最左侧节点
  10. if (cur.left != null) nodeQueue.offer(cur.left);
  11. if (cur.right != null) nodeQueue.offer(cur.right);
  12. }
  13. }
  14. return left;
  15. }

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. eclipse开发工具之"导入项目"

    1.选择菜单栏"file""下的"import" 2.选择Maven 在选中"Existing Maven Projects",然 ...

  2. 如何更愉快地使用rem —— 别说你懂CSS相对单位

    前段时间试译了Keith J.Grant的CSS好书<CSS in Depth>,其中的第二章<Working with relative units>,书中对relative ...

  3. Android项目如何打包成安装包.apk

    1.Build -> Generate Signed APK 2.APK -> Next 3.输入Key store path以及密码 ->next(如果是第一次则需要新建,新建教程 ...

  4. linux vim编辑器使用

    小i 在光标所在行位置停止不动开始写入内容 大I 在光标所在行行首开始写入内容 小a 在光标所在行当前字符后开始写入内容 大A 在光标所在行行尾开始写入内容 小o 在光标所在行下一行开始写入内容 大O ...

  5. 数据库number(4,3)表示什么

    1 你看 number(4,3)是表示 这个数 一共有4位是有效位,后面的3 表示有3个是小数也就是这个数 只能是1.234,这样格式的 最大只能是9.999,2 number(3,4) 表示这个数 ...

  6. location中的各个属性

    http://172.16.20.218:8080/m/MGU20201126001-001/index.html?username=admin&password=123#/write   浏 ...

  7. EMS设置发送连接器和接收连接器邮件大小

    任务:通过EMS命令设置发送接收连接器和接收连接器的邮件大小限制值为50MB. 以Exchange管理员身份打开EMS控制台.在PowerShell命令提示符下. 键入以下命令设置接收-连接器的最大邮 ...

  8. 《头号玩家》AI电影调研报告(四)

    五. 现实中对于技术的实现 1.自由变身 电影中,主人公借助"魔幻眼镜"让自己快速变成另一个人的模样,现实中我们一样也能做到! 在今年游戏开发者大会(GDC)上,一个名为" ...

  9. WIN进程注入&BypassUAC&令牌窃取

    WIN进程注入&BypassUAC&令牌窃取 本地提权-win令牌窃取 假冒令牌可以假冒一个网络中的另一个用户进行各类操作. 所以当一个攻击者需要域管理员的操作权限时候,需通过假冒域管 ...

  10. python---用顺序表实现双端队列

    class Dqueue(object): """双端队列""" def __init__(self): self.__list = [] ...