LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)
513. 找树左下角的值
513. Find Bottom Left Tree Value
题目描述
给定一个二叉树,在树的最后一行找到最左边的值。
LeetCode513. Find Bottom Left Tree Value中等
示例 1:
输入:
2
/ \
1 3
输出:
1
示例 2:
输入:
1
/ \
2 3
/ / \
4 5 6
/
7
输出:
7
注意: 您可以假设树(即给定的根节点)不为 NULL。
解答思路
Java 实现
TreeNode Class
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
import java.util.LinkedList;
import java.util.Queue;
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
root = queue.poll();
if (root.right != null) {
queue.add(root.right);
}
if (root.left != null) {
queue.add(root.left);
}
}
return root.val;
}
}
参考资料
- https://www.cnblogs.com/grandyang/p/6405128.html
- https://leetcode.com/problems/find-bottom-left-tree-value/
- https://leetcode-cn.com/problems/find-bottom-left-tree-value/
LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)的更多相关文章
- Java实现 LeetCode 513 找树左下角的值
513. 找树左下角的值 给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输 ...
- [Swift]LeetCode513. 找树左下角的值 | 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之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)
Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
- 领扣(LeetCode)找树左下角的值 个人题解
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...
- 513 Find Bottom Left Tree Value 找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...
- Leetcode513. Find Bottom Left Tree Value找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- [LeetCode] 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 ...
随机推荐
- SpringBoot源码分析-编译环境与新建测试模块
建议 分析源码建议不要使用Idea或者Eclipse等IDE工具的反编译功能或者导入源码包的方式看源码,那样不能给框架的源码做注释,所以分析源码之前都得先下载源码并构建,然后在项目中新建一个Modul ...
- super()函数
1.简单的使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如: 在上面,A ...
- [Javascript] Sort by multi factors
For example, we have a 2D arrays; const arys = [ [], [], [] ]; We want to sort by the number first, ...
- LOJ P10130 点的距离 题解
这道题相当于倍增求LCA的板子,我们只要构建一棵树,然后距离就是x的深度+y的深度 - LCA(x,y)的深度: #include<iostream> #include<cstdio ...
- 堆内存腐败异常(STATUS_HEAP_CORRUPTION---0xC0000374)
什么是内存腐败 当堆内存位置的内容由于编程行为而被修改,超出了原始程序构造的意图时,计算机程序就会发生内存腐败,也可以叫内存破坏:这被称为违反内存安全.内存腐败的最可能原因是编程错误.当腐败的内存内容 ...
- Omnibus-ctl: What is it and what can it do for you?
转自:https://blog.chef.io/2015/05/26/omnibus-ctl-what-is-it-and-what-can-it-do-for-you/ Are you buildi ...
- 洛谷 P2813【母舰】 题解
总体思路: 输入护盾和攻击力,然后快速排序sort走起来, 排完序之后从第一个开始找,如果攻击力大于护盾,护盾继续下一个, 这个攻击力记录为0,如果小雨的话,那就攻击力继续下一个,护盾不动, 其中最为 ...
- GoCN每日新闻(2019-10-09)
GoCN每日新闻(2019-10-09) GoCN每日新闻(2019-10-09) 1. 我们如何将服务延迟减少了98% https://blog.gojekengineering.com/the-n ...
- OpenFOAM——高空腔内的湍流自然对流
本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL052: Turbulent Natural Convection Inside ...
- POP IM 产品分析报告
一. 体验环境 产品名称:POP IM 软件版本:v2.4.0 手机系统:一加5T Android 9 体验时间:2019.10.22-2019.10.31 二. 产品简介 1. 产品定位 ...