当前位置:网站首页>leetcode刷题:二叉树15(找树左下角的值)
leetcode刷题:二叉树15(找树左下角的值)
2022-07-05 19:52:00 【涛涛英语学不进去】
513.找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值。
示例 1:

示例 2:

…不就是层序遍历最后一行第一个嘛
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
/** * @ClassName FindBottomLeftValue * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 11:26 * @Version 1.0 * https://leetcode.cn/problems/find-bottom-left-tree-value/ * 513. 找树左下角的值 **/
public class FindBottomLeftValue {
/** * 给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。 * 假设二叉树中至少有一个节点。 * <p> * ..不就是层序遍历最后一行第一个嘛 * @param root * @return */
public int findBottomLeftValue(TreeNode root) {
if (root.left == null && root.right == null) {
return root.val;
}
int result = root.val;
Deque<TreeNode> deque = new ArrayDeque();
deque.offer(root);
while (!deque.isEmpty()) {
//每层的开始
result = deque.peek().val;
int size = deque.size();
while (size > 0) {
root = deque.pop();
if (root.left != null) {
deque.offer(root.left);
}
if (root.right != null) {
deque.offer(root.right);
}
size--;
}
//每层的结束
}
return result;
}
}

边栏推荐
- [AI framework basic technology] automatic derivation mechanism (autograd)
- 建议收藏,我的腾讯Android面试经历分享
- openh264解码数据流向分析
- Fundamentals of shell programming (Chapter 9: loop)
- Redis cluster simulated message queue
- Hiengine: comparable to the local cloud native memory database engine
- 爬虫练习题(二)
- PHP uses ueditor to upload pictures and add watermarks
- MMO项目学习一:预热
- 建立自己的网站(16)
猜你喜欢

S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL

淺淺的談一下ThreadLocalInsecureRandom

全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云

如何安全快速地从 Centos迁移到openEuler

Add data to excel small and medium-sized cases through poi
The problem of returning the longtext field in MySQL and its solution

CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)

Build your own website (16)

What does software testing do? What are the requirements for learning?
随机推荐
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Recommended collection, my Tencent Android interview experience sharing
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
Debezium series: parsing the default value character set
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
Common operators and operator priority
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
常用运算符与运算符优先级
Postman core function analysis - parameterization and test report
建议收藏,我的腾讯Android面试经历分享
Force buckle 729 My schedule I
信息/数据
力扣 1200. 最小绝对差
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
Concept and syntax of function
Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value