当前位置:网站首页>[513. find the value in the lower left corner of the tree]
[513. find the value in the lower left corner of the tree]
2022-06-22 21:22:00 【Sugar_ wolf】
source : Power button (LeetCode)
describe :
Given a binary tree The root node root, Please find the of the binary tree At the bottom Leftmost The value of the node .
Suppose there is at least one node in the binary tree .
Example 1:

Input : root = [2,1,3]
Output : 1
Example 2:

Input : [1,2,3,4,null,5,6,null,null,7]
Output : 7
Tips :
- The range of the number of nodes in a binary tree is [1,104]
- -231 <= Node.val <= 231 - 1
Method 1 : Depth-first search
Use height Record the height of the node traversed , curVal Record height at curHeight The value of the leftmost node of . In depth first search , First, we search the left child node of the current node , Then search the right child node of the current node , Then judge the height of the current node height Is it greater than curHeight, If it is , It will be curVal Set to the value of the current node , curHeight Set to height.
Because we first traverse the left subtree , And then traverse the right subtree , So for all nodes at the same height , The leftmost node must be the first to be traversed .
Code :
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;
}
};
Execution time :4 ms, In all C++ Defeated in submission 97.07% Users of
Memory consumption :21 MB, In all C++ Defeated in submission 97.09% Users of
Complexity analysis
Time complexity : O(n), among n Is the number of nodes in the binary tree . Need to traverse n Nodes .
Spatial complexity : O(n). Recursive stack needs to occupy O(n) Space .
Method 2 : Breadth first search
Use breadth first search to traverse the nodes of each layer . When traversing a node , You need to put its non empty right child node into the queue first , Then put its non empty left child nodes into the queue , In this way, the nodes of each layer can be traversed from right to left . The value of the last node traversed by the breadth first search is the value of the lowest left node .
Code :
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;
}
};
Execution time :8 ms, In all C++ Defeated in submission 81.05% Users of
Memory consumption :21.1 MB, In all C++ Defeated in submission 62.89% Users of
author:LeetCode-Solution Add link description
边栏推荐
- [21. merge two ordered linked lists]
- Agricultural futures account opening
- 53 page intelligent campus intelligent system design scheme (download attached)
- 百家讲坛 黄帝内经(第一部)
- Golang learning notes - structure
- 已解决:一个表中可以有多个自增列吗
- [20. valid brackets]
- 【文末送书】火遍全网的AI给老照片上色,这里有一份详细教程!
- C语言中int和char的对应关系
- 扩展Ribbon支持Nacos权重的三种方式
猜你喜欢

R language airpassengers dataset visualization

云服务器中安装mysql(2022版)

53 page intelligent campus intelligent system design scheme (download attached)

The access succeeds but an exception is thrown: could not find acceptable representation

Resolved: can there be multiple auto incrementing columns in a table
![[redis]集群与常见错误](/img/a5/94906b62b1ec0d549f9b72ff3db7f2.png)
[redis]集群与常见错误

基于C语言开发工资管理系统 课程论文+源码及可执行exe文件
![[redis] profile](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis] profile

Visualization of R language nutrient dataset

Set up your own website (12)
随机推荐
[redis] profile
Easyclick update Gallery
[redis]redis的持久化操作
嵌入式开发基础之任务管理(线程管理)
[cm11 linked list splitting]
[redis] cluster and common errors
Feign常见问题总结
R language Midwest dataset visualization
Final review of scientific and technological literature of Shandong University (Personal Crash Course)
PHP image making
Operation of simulation test platform for 2022 Shandong safety officer C certificate test
百家讲坛 黄帝内经(第一部)
微信小程序批量提交审核
Huawei cloud releases Latin American Internet strategy
Evaluation index and code realization (ndcg)
2022化工自动化控制仪表考试练习题及在线模拟考试
[138. copy linked list with random pointer]
R language universalbank CSV "data analysis
Visualization of R language nutrient dataset
Notes d'apprentissage de golang - structure
