当前位置:网站首页>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;
}
}

边栏推荐
- 95后阿里P7晒出工资单:狠补了这个,真香...
- C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
- [Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
- 【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
- 国信证券在网上开户安全吗?
- 测试外包公司怎么样?
- [AI framework basic technology] automatic derivation mechanism (autograd)
- 如何安全快速地从 Centos迁移到openEuler
- 信息/数据
- Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
猜你喜欢

Reflection and imagination on the notation like tool

众昂矿业:2022年全球萤石行业市场供给现状分析

集合

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

Postman核心功能解析-参数化和测试报告

How to apply smart contracts more wisely in 2022?

That's awesome. It's enough to read this article

IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍

The city chain technology Digital Innovation Strategy Summit was successfully held

Worthy of being a boss, byte Daniel spent eight months on another masterpiece
随机推荐
不愧是大佬,字节大牛耗时八个月又一力作
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
The difference between ID selector and class selector
使用 RepositoryProvider简化父子组件的传值
Necessary skills for interview in large factories, 2022android will not die, I will not fall
挖财钱堂教育靠谱安全吗?
四万字长文说operator new & operator delete
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
Relationship between floating elements and parent and brother boxes
Force buckle 1200 Minimum absolute difference
redis集群模拟消息队列
PG basics -- Logical Structure Management (user and permission management)
Xaas trap: all things serve (possible) is not what it really needs
Postman core function analysis - parameterization and test report
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
Is it safe to open a mobile stock account? Is it reliable?
What does software testing do? What are the requirements for learning?
打新债在哪里操作开户是更安全可靠的呢
Reptile exercises (II)