当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢
随机推荐
如何修改gif图片尺寸?教你一键裁剪gif尺寸
sprintboot验证码kaptcha 自定义图片样式
Copper lock password library
拆分整数为2的幂次项和 → 理解多重背包问题二进制优化的核心思想
清理空的 Jetpack Compose 应用程序模板
如何将jpg静图做成gif动图?教你1分钟快速合成gif
二维费用背包问题的解题套路
C语言各种符号如何使用
8月Meetup | “数据调度+分析引擎”解锁企业数字化转型之路
Spike project harvest
app自动化测试webview怎么操作
Go+:首个顺应 “三位一体” 发展潮流的编程语言
玩转Redis|学会这10点让你分分钟拿下Redis,满足你的一切疑问
Gif动图制作怎么在线操作?一招教你快速完成gif在线制作
险资又做LP,一出手40亿
babylonjs shader
FTXUI基础笔记(botton按钮组件进阶)
Colocate Join :ClickHouse的一种高性能分布式join查询模型
Mastodon:可创建类似推特的开源社交网络服务器
一文带你彻底拿下a,b两点间等效电阻