当前位置:网站首页>Leetcode 513. 找树左下角的值
Leetcode 513. 找树左下角的值
2022-06-25 22:03:00 【cwtnice】
原题链接:Leetcode 513. Find Bottom Left Tree Value
Given the root of a binary tree, return the leftmost value in the last row of the tree.
Example 1:
Input: root = [2,1,3]
Output: 1
Example 2:

Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7
Constraints:
- The number of nodes in the tree is in the range [1, 104].
- -231 <= Node.val <= 231 - 1
做法一:dfs
思路:
用两个变量记录当前的节点值以及当前节点的最大高度。
每往下一层高度加一,并递归左右子树。(有限递归左子树能保证在同一高度情况下答案一定是最左边的)
如果高度超过了记录的高度,就更新节点值以及最大高度
c++代码:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
void dfs(TreeNode* root, int height, int &ans, int &maxHeight){
if(root == nullptr)
return;
// 到下一层, 先递归左孩子, 再递归右孩子
height++;
dfs(root->left, height, ans, maxHeight);
dfs(root->right, height, cuansrVal, maxHeight);
// 当高度比最大高度还大时, 更新
if(height > maxHeight){
ans = root->val;
maxHeight = height;
}
}
int findBottomLeftValue(TreeNode* root) {
// ans为最底层最左边的节点值 maxHeight为最大深度
int ans, maxHeight = 0;
dfs(root, 0, ans, maxHeight);
return ans;
}
};
复杂度分析:
- 时间复杂度:O(n),会遍历每一个节点
- 空间复杂度:O(n),当所有节点都在一边时,递归栈的最大深度就是n
边栏推荐
- 字符串
- OBS-Studio-27.2.4-Full-Installer-x64. Exe Download
- Spark日志分析
- 史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
- Windows安装Redis及简单使用
- What is Unified Extensible Firmware Interface (UEFI)?
- Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
- QT Chinese and English use different fonts respectively
- 1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
- cookie、session、token
猜你喜欢

Uni app -- listen for the exit of the return key

毕业旅行 | 伦敦5日游行程推荐

hiberate核心API/配置文件/一级缓存详解

YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21

Audio basics and PCM to WAV

Architecture part -- the use of UMI framework and DVA

Database - mongodb

后序线索二叉树

社招两年半10个公司28轮面试面经(含字节、拼多多、美团、滴滴......)

Graduation trip | recommended 5-day trip to London
随机推荐
Qt Utf8 与 Unicode 编码的互相转换, Unicode编码输出为格式为 &#xXXXX
BI-SQL丨存储过程(一)
Solving typeerror: Unicode objects must be encoded before hashing
Windows redis installation and simple use
C. Planar Reflections-CodeCraft-21 and Codeforces Round #711 (Div. 2)
The software test interview has been suspended. The interviewer always says that the logical thinking is chaotic. What should I do?
关于go协程超时退出控制条件与方式分析
经典图像分割网络:Unet 支持libtorch部署推理【附代码】
转载: QTableWidget详解(样式、右键菜单、表头塌陷、多选等)
Reproduction of an implant found by Kaspersky that writes shellcode into evenlog
Uni app -- listen for the exit of the return key
Audio basics and PCM to WAV
UE4 学习记录二 给角色添加骨架,皮肤,及运动动画
CSDN force value
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
QT custom implemented calendar control
Day4 branch and loop summary and operation
golang Make a list of intervals with sequential numbers
debezium
后序线索二叉树