当前位置:网站首页>【513. 找树左下角的值】
【513. 找树左下角的值】
2022-06-22 19:11:00 【Sugar_wolf】
来源:力扣(LeetCode)
描述:
给定一个二叉树的 根节点 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
方法一:深度优先搜索
使用 height 记录遍历到的节点的高度, curVal 记录高度在 curHeight 的最左节点的值。在深度优先搜索时,我们先搜索当前节点的左子节点,再搜索当前节点的右子节点,然后判断当前节点的高度 height 是否大于 curHeight,如果是,那么将 curVal 设置为当前结点的值, curHeight 设置为 height。
因为我们先遍历左子树,然后再遍历右子树,所以对同一高度的所有节点,最左节点肯定是最先被遍历到的。
代码:
class Solution {
public:
void dfs(TreeNode *root, int height, int &curVal, int &curHeight) {
if (root == nullptr) {
return;
}
height++;
dfs(root->left, height, curVal, curHeight);
dfs(root->right, height, curVal, curHeight);
if (height > curHeight) {
curHeight = height;
curVal = root->val;
}
}
int findBottomLeftValue(TreeNode* root) {
int curVal, curHeight = 0;
dfs(root, 0, curVal, curHeight);
return curVal;
}
};
执行用时:4 ms, 在所有 C++ 提交中击败了97.07%的用户
内存消耗:21 MB, 在所有 C++ 提交中击败了97.09%的用户
复杂度分析
时间复杂度: O(n),其中 n 是二叉树的节点数目。需要遍历 n 个节点。
空间复杂度: O(n)。递归栈需要占用 O(n) 的空间。
方法二:广度优先搜索
使用广度优先搜索遍历每一层的节点。在遍历一个节点时,需要先把它的非空右子节点放入队列,然后再把它的非空左子节点放入队列,这样才能保证从右到左遍历每一层的节点。广度优先搜索所遍历的最后一个节点的值就是最底层最左边节点的值。
代码:
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
int ret;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
auto p = q.front();
q.pop();
if (p->right) {
q.push(p->right);
}
if (p->left) {
q.push(p->left);
}
ret = p->val;
}
return ret;
}
};
执行用时:8 ms, 在所有 C++ 提交中击败了81.05%的用户
内存消耗:21.1 MB, 在所有 C++ 提交中击败了62.89%的用户
author:LeetCode-Solution添加链接描述
边栏推荐
- Introduction of Neural Network (BP) in Intelligent Computing
- One picture decoding opencloudos community open day
- Can financial products be redeemed on weekends?
- 采用QTest进行数据集测试-性能测试-GUI测试
- How to calculate yoy and mom in MySQL
- An IPFs enabled email - skiff
- Introduction to async profiler
- 软件压力测试有哪些方法,如何选择软件压力测试机构?
- Raspberry pie environment settings
- Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem
猜你喜欢

Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)

Containerd容器运行时(2):yum安装与二进制安装,哪个更适合你?

一个支持IPFS的电子邮件——SKIFF
Gradle Build Cache引发的Task缓存编译问题

他98年的,我玩不过他...

Raspberry pie environment settings

【观察】软件行业创新进入“新周期”,如何在变局中开新局?

市场开始降温,对NFT 是坏事么?

天,靠八股文逆袭了啊

ROS从入门到精通(八) 常用传感器与消息数据
随机推荐
MYSQL 几个常用命令使用
软件测试——测试用例设计&测试分类详解
Be careful with MySQL filesort
[deeply understand tcapulusdb technology] create a game zone
Connect function usage of socket
ZABBIX learning notes (37)
MySQL基础——约束
完全背包如何考虑排列问题
[in depth understanding of tcapulusdb technology] tcapulusdb adds a new business cluster cluster
An IPFs enabled email - skiff
Teach you how to create SSM project structure in idea
[proteus simulation] 74LS138 decoder water lamp
【Proteus仿真】74LS138译码器流水灯
Comment le sac à dos complet considère - t - il la disposition?
Oh, my God, it's a counter attack by eight part essay
【深入理解TcaplusDB技术】TcaplusDB运维
ROS from entry to mastery (VIII) common sensors and message data
JWT简介
A detailed solution to mysql8.0 forgetting password
用RNN & CNN进行情感分析 - PyTorch
