当前位置:网站首页>LeetCode-513. 找树左下角的值
LeetCode-513. 找树左下角的值
2022-06-24 19:27:00 【边界流浪者】
给定一个二叉树的 根节点 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
#include<iostream>
using namespace std;
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 depth) {
if (root == NULL) {
return;
}
/* 查找最深的节点 */
if (depth > max) {
max = depth;
result = root->val;
}
/* 优先走最左边 */
if (root->left) {
dfs(root->left, depth + 1);
}
if (root->right) {
dfs(root->right, depth + 1);
}
}
int findBottomLeftValue(TreeNode* root) {
max = 1;
result = root->val;
dfs(root, 1);
return result;
}
private:
int max;
int result;
};
int main() {
TreeNode* r = new TreeNode(1);
r->left = new TreeNode(2);
r->left->left = new TreeNode(4);
r->right = new TreeNode(3);
r->right->left = new TreeNode(5);
r->right->right = new TreeNode(6);
r->right->left->left = new TreeNode(7);
shared_ptr<Solution> ptr = std::make_shared<Solution>();
cout << ptr->findBottomLeftValue(r) << endl;
system("pause");
return 0;
}
边栏推荐
- Three more days
- Pattern recognition - 1 Bayesian decision theory_ P1
- 【吴恩达笔记】卷积神经网络
- 使用Adb连接设备时提示设备无权限
- AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
- Docking of arkit and character creator animation curves
- Please open online PDF carefully
- Oauth2.0 introduction
- [cloud native learning notes] kubernetes practice command
- memcached全面剖析–5. memcached的应用和兼容程序
猜你喜欢
Alibaba cloud lightweight servers open designated ports
2022国际女性工程师日:戴森设计大奖彰显女性设计实力
【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
ping: www.baidu.com: 未知的名称或服务
Shengzhe technology AI intelligent drowning prevention service launched
leetcode-201_2021_10_17
Big factories go out to sea and lose "posture"
福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
Visit Amazon memorydb and build your own redis memory database
2022 international women engineers' Day: Dyson design award shows women's design strength
随机推荐
leetcode1720_2021-10-14
BBR bandwidth per second conversion logic
推荐模型之多任务模型:ESMM、MMOE
【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程
Volcano成Spark默认batch调度器
Three more days
Notes_ Vlan
Why are life science enterprises on the cloud in succession?
Volcano becomes spark default batch scheduler
leetcode_191_2021-10-15
Understanding openstack network
Slider控制Animator动画播放进度
Graduation summary of phase 6 of the construction practice camp
Shengzhe technology AI intelligent drowning prevention service launched
Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached
leetcode-201_2021_10_17
BPF_ PROG_ TYPE_ SOCKET_ Filter function implementation
Please open online PDF carefully
The first day of handwritten RPC -- review of some basic knowledge
[camera Foundation (I)] working principle and overall structure of camera