当前位置:网站首页>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;
}
}
边栏推荐
- 测试的核心价值到底是什么?
- Django uses mysqlclient service to connect and write to the database
- Where is the operation of new bonds? Is it safer and more reliable to open an account
- 软件测试工程师是做什么的?待遇前景怎么样?
- 【obs】libobs-winrt :CreateDispatcherQueueController
- 40000 word Wenshuo operator new & operator delete
- Multi branch structure
- Force buckle 1200 Minimum absolute difference
- Reptile exercises (II)
- What do software test engineers do? How about the prospect of treatment?
猜你喜欢
40000 word Wenshuo operator new & operator delete
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
Android interview classic, 2022 Android interview written examination summary
面试官:Redis中集合数据类型的内部实现方式是什么?
Xaas trap: all things serve (possible) is not what it really needs
深度學習 卷積神經網絡(CNN)基礎
[AI framework basic technology] automatic derivation mechanism (autograd)
测试的核心价值到底是什么?
redis集群模拟消息队列
PHP uses ueditor to upload pictures and add watermarks
随机推荐
成功入职百度月薪35K,2022Android开发面试解答
【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
JVMRandom不可设置种子|问题追溯|源码追溯
How to apply smart contracts more wisely in 2022?
Which securities company is better and which platform is safer for mobile account opening
MMO project learning 1: preheating
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
手机股票开户安全吗?靠不靠谱啊?
Postman核心功能解析-参数化和测试报告
力扣 729. 我的日程安排表 I
Is the education of caiqiantang reliable and safe?
信息/数据
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Using repositoryprovider to simplify the value passing of parent-child components
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
gst-launch的-v参数