当前位置:网站首页>力扣每日一题leetcode 513. 找树左下角的值
力扣每日一题leetcode 513. 找树左下角的值
2022-07-27 05:21:00 【最后一只三脚兽】
思路 简单的深度遍历,结合了二叉树后序遍历的思想。先不断向下遍历,向下遍历时先向左遍历(保证得到的最深的值是在最左边),如果发现遍历的深度大于已有的深度就把最大深度改为当前深度,值也修改。
class Solution {
int val;
int deep = -1;
public void dfs(TreeNode root,int depth){
if(root == null)return;
dfs(root.left,depth+1);//向左遍历
dfs(root.right,depth+1);//向右遍历
if(depth > deep){
//代码运行到这里时该节点的子节点已经全部遍历过了,不理解可以看看二叉树的后序遍历,核心就是回溯
deep = depth;//修改深度
val = root.val;//修改值
}
}
public int findBottomLeftValue(TreeNode root) {
dfs(root,0);
return val;
}
}
边栏推荐
- 力扣 110. 平衡二叉树
- Essential tool for making video special effects: nuke 13
- 2022.6.10 stm32mp157 serial port clock learning
- System Design的相关准备材料
- 韦东山 数码相框 项目学习(一)在LCD上显示ASCII字符
- 力扣160. 相交链表
- WebODM win10安装教程(亲测)
- [first song] rebirth of me in py introductory training (2): formula programming
- socket编程二:使用select
- 编程学习记录——第8课【数组与设计五子棋,扫雷游戏】
猜你喜欢

能替代ps的修图软件?

Super remote connection management tool: Royal TSX

arcgis for js api(2) 获取要素服务的id集合

发布 分辨率0.22m的建筑物分割数据库

Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis
Acwing the number of square arrays of one question per day

STM32 infrared remote control
![[first song] machine learning of rebirth - linear regression](/img/70/3efd9eacf88f55022eb52d096926f7.png)
[first song] machine learning of rebirth - linear regression

Leetcode每日一题30. 串联所有单词的子串

2021-06-26
随机推荐
力扣题解 动态规划(4)
子类调用父类构造函数的时机
【头歌】重生之数据科学导论——回归进阶
编程学习记录——第7课【函数】
What has been updated in the Chinese version of XMIND mind map 2022 v12.0.3?
1半自动爬虫
arcgis for js api-入门系列
[song] rebirth of me in py introductory training (7): function call
Day 2. Depressive symptoms, post-traumatic stress symptoms and suicide risk among graduate students
【头歌】重生之我在py入门实训中(2):公式编程
力扣题解 二叉树(6)
【头歌】重生之我在py入门实训中(5):列表
小技巧-彻底删除U盘中的文件
[song] rebirth of me in py introductory training (12): Matplotlib interface and common graphics
【5·20特辑】MatLAb之我在和你表白
[first song] machine learning of rebirth - linear regression
LaTeX中多个公式公用一个序号时
c语言-线性顺序表
Gbase 8C - SQL reference 6 SQL syntax (14)
力扣题解 单调栈

