当前位置:网站首页>The sword refers to OfferⅡ 045. The bottommost leftmost value of the binary tree dfs
The sword refers to OfferⅡ 045. The bottommost leftmost value of the binary tree dfs
2022-08-10 16:48:00 【HotRabbit.】
题目
给定一个二叉树的 根节点root,请找出该二叉树的 最底层 最左边 节点的值.
假设二叉树中至少有一个节点.
示例 1:

输入: root = [2,1,3]
输出: 1
示例 2:

输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7
提示:
- 二叉树的节点个数的范围是
[1,104] -231 <= Node.val <= 231 - 1
注意:本题与主站 513 题相同: https://leetcode-cn.com/problems/find-bottom-left-tree-value/
Related Topics
- 树
- 深度优先搜索
- 广度优先搜索
- 二叉树
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/LwUNpT
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
思路
定义两个全局变量,Record the height and the current value that matches the meaning of the question,深度遍历,When traversing a node above the current height,记录当前值,先dfs左节点,再dfs右节点,This way if the left node has a value,The right node is not as tall as the left node,won't get.
题解
class Solution {
int curVal = 0;
int curHeight = 0;
public int findBottomLeftValue(TreeNode root) {
int curHeight = 0;
dfs(root,0);
return curVal;
}
public void dfs(TreeNode root, int height){
if (root == null) return;
height++;
dfs(root.left,height);
dfs(root.right,height);
if (height > curHeight){
curHeight = height;
curVal = root.val;
}
}
}
边栏推荐
猜你喜欢
随机推荐
二维费用背包问题的解题套路
LeetCode-922. Sort Array By Parity II
String compression (3) short string compression
生成树协议(STP---Spanning Tree Protocol)
力扣+牛客--刷题记录
C#去水印软件源代码
如何使用Swift Package插件生成代码
让页面滚动到指定位置
C语言按位运算符如何使用
app自动化测试webview怎么操作
【Windows】将排除项添加到安全中心以避免exe被系统自动删除
年薪60万+?这份10万字的面试突击宝典涵盖阿里 P5 工程师~P7 所有技术栈
Andorid源码编译需要掌握的shell语法(三)
如何将jpg图片变成gif?教你一分钟图片合成gif的方法
关于Web渗透测试需要知道的一切:完整指南
64位 RT-Thread 移植到 Cortex-A53 系统 bug 修复笔记
聊聊云原生数据平台
LeetCode-101. Symmetric Tree
积分可以当钱用,阿里推出个人「碳账户」
MySQL数据库完全备份与增量备份怎么实现









