当前位置:网站首页>513. Find Bottom Left Tree Value
513. Find Bottom Left Tree Value
2022-06-23 15:22:00 【SUNNY_CHANGQI】
The description of the porblem
Given the root of a binary tree, return the leftmost value in the last row of the tree.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-bottom-left-tree-value
an example

The intuition for this
leverage the broadcast priority search to traversal all the elements in the TREE. In addition, traverse the sub-right tree, then the sub-left tree.
The codes
#include <queue>
#include <iostream>
#include <vector>
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:
int findBottomLeftValue(TreeNode* root) {
vector<int> values;
queue<TreeNode *> qu;
qu.push(root);
while (!qu.empty()) {
TreeNode *tmp_node = qu.front();
values.emplace_back(tmp_node->val);
qu.pop();
if (tmp_node->right) {
qu.push(tmp_node->right);
}
if (tmp_node->left) {
qu.push(tmp_node->left);
}
}
return *(values.end() - 1);
}
};
int main()
{
TreeNode *head = new TreeNode(2);
head->left = new TreeNode(1);
head->right = new TreeNode(3);
Solution s;
int res = s.findBottomLeftValue(head);
std::cout << "The res:" << res;
return 0;
}
The corresponding results
Starting program: /mnt/c/Users/sunny/Desktop/practices for cmake/test
The res:1[Inferior 1 (process 1143) exited normally]
边栏推荐
猜你喜欢

Gartner's latest report: development of low code application development platform in China

MySQL series: overview of the overall architecture

Volatile~ variables are not visible under multithreading

FPN特征金字塔网络

任何代码未动的情况下第二天项目访问速度明显下降,案例分析

JS垃圾回收

30. 串联所有单词的子串

A transformer can only convert alternating current. How can I convert direct current?

golang 重要知识:mutex

Introduction to the push function in JS
随机推荐
Nfnet: extension of NF RESNET without BN's 4096 super batch size training | 21 year paper
139. word splitting
C. Set or Decrease-Educational Codeforces Round 120 (Rated for Div. 2)
System design and analysis - Technical Report - a solution for regularly clearing verification code
F5 application strategy status report in 2022: edge deployment and load security become the focus of attention in the Asia Pacific Region
[opencv450] salt and pepper noise demo
Important knowledge of golang: waitgroup parsing
mysql事务与锁
A transformer can only convert alternating current. How can I convert direct current?
Top 10 purchase, sales and inventory software rankings!
golang 重要知识:mutex
Why can a high pass filter become a differentiator?
JSON——学习笔记(消息转换器等)
Jsr303 data verification
spdlog记录日志示例 - 使用sink创建logger
[普通物理] 半波损失 等厚与等倾干涉
139. 单词拆分
自监督学习(SSL)Self-Supervised Learning
PHP specified fields are more than 100 in positive order and less than 100 in random order
VIM backup history command