当前位置:网站首页>Li Kou today's question 513 Find the value in the lower left corner of the tree
Li Kou today's question 513 Find the value in the lower left corner of the tree
2022-06-23 04:54:00 【Struggling young man】
513. Find the value in the lower left corner of the tree
The idea is simple , Traverse the left and right subtrees respectively ( The former sequence traversal ), The first node encountered when reaching the maximum depth is The bottom and the left The node of .
Set a variable maxDepth Used to record Trees The maximum depth of , Set another variable depth To record the current traversal depth , The initial values of both values are set to 0, Set up a tree node , Used to save the result node .
In the process of recursive traversal , If you find the depth of the current traversal depth Be greater than maxDepth The maximum depth of , Then give it to me maxDepth assignment , Make sure its data is always updated , And always the biggest . At the same time let res Always point to the deepest node .
class Solution {
// Record the maximum depth of the tree
int maxDepth = 0;
// Record traverse The depth traversed recursively
int depth = 0;
// Result node , It is used to save the bottom left node
TreeNode res = null;
public int findBottomLeftValue(TreeNode root) {
traverse(root);
return res.val;
}
void traverse(TreeNode root){
if(root == null){
return ;
}
// The position of preorder traversal
depth++;
if(depth > maxDepth){
// When the maximum depth is reached , The first node encountered is the bottom left node
maxDepth = depth;
res = root;
}
traverse(root.left);
traverse(root.right);
// Postorder traversal position , Return to root node , Traverse depth minus one
depth--;
}
}
边栏推荐
- OGNL Object-Graph Navigation Language
- Transformers中的动态学习率
- 微信小程序实例开发:跑起来
- ADR electronic transmission EDI solution of national adverse drug reaction monitoring center
- Abnova LiquidCell-负富集细胞分离和回收系统
- Abnova acid phosphatase (wheat germ) instructions
- 实战| 记一次借Viper来多重内网渗透
- FreeModBus解析1
- E45: ‘readonly‘ option is set (add ! to override)
- Principle of 8-bit full adder
猜你喜欢
随机推荐
项目总结1(头文件,switch,&&,位变量)
Altium designer 09 screen printing displays a green warning near the pad. How to prevent it from alarming?
DSP7 环境
Static two position relay xjls-84/440/dc220v
win10查看my.ini路径
Pta:6-71 clock simulation
一款MVC5+EasyUI企业快速开发框架源码 BS框架源码
[paper reading] semi supervised learning with ladder networks
Pta:7-58 Book audio-visual rental management
Current relay jdl-1002a
32 single chip microcomputer has more than one variable Used in C
Abnova ABCB10(人)重组蛋白说明书
Notes on writing questions in C language -- free falling ball
Abnova ACTN4纯化兔多克隆抗体说明书
Volatile and threads
Openjudge noi 1.13 51: ancient password
Openwrt directory structure
在Pycharm中使用append()方法对列表添加元素时提示“This list creation could be rewritten as a list literal“的解决方法
聊聊 C# 中的 Composite 模式
395. 冗余路径







