当前位置:网站首页>513. Find Bottom Left Tree Value
513. Find Bottom Left Tree Value
2022-06-23 15:54: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.
source : Power button (LeetCode)
link :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]
边栏推荐
- golang 重要知识:waitgroup 解析
- F5 application strategy status report in 2022: edge deployment and load security become the focus of attention in the Asia Pacific Region
- The meaning of FPGA abbreviations and words in engineering field
- Stone from another mountain - Intelligent Question and answer technology in wechat search
- The idea and method of MySQL master-slave only synchronizing some libraries or tables
- 图片读取:Image.open(ImgPath)
- How strong is Jingdong's takeout after entering meituan and starving the hinterland?
- [普通物理] 半波损失 等厚与等倾干涉
- 32. compose beautiful touch animation
- 139. Séparation des mots
猜你喜欢

Shandong: food "hidden money", consumption "sweeping monk"
![[普通物理] 光的衍射](/img/1a/20dbd15e0c8c91a3e59753b2f6797a.png)
[普通物理] 光的衍射

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

golang 重要知识:RWMutex 读写锁分析

英特尔Arc A380显卡消息汇总:跑分亮眼驱动拉胯 入门性价产品亟待优化

Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)

Sleuth + Zipkin

golang 重要知识:定时器 timer

Diffraction of light

js的slice()和splice()
随机推荐
Origin of sectigo (Comodo) Certificate
电子学会图形化一级编程题解析:猫捉老鼠
Android kotlin collaboration Async
C. Set or Decrease-Educational Codeforces Round 120 (Rated for Div. 2)
30. concatenate substrings of all words
《Apache Commons 工具类》
The meaning of FPGA abbreviations and words in engineering field
Analysis of TCP three-time handshake and four-time handshake
readImg: 读取图片到Variable变量
自监督学习(SSL)Self-Supervised Learning
2022年个人理财利率是多少?个人如何选择理财产品?
Gartner's latest report: development of low code application development platform in China
MySQL日志管理怎么配置
他山之石 | 微信搜一搜中的智能问答技术
golang 重要知识:atomic 原子操作
ABP框架之——数据访问基础架构(下)
Why can a high pass filter become a differentiator?
PHP 2D array insert
F5《2022年应用策略现状报告》:边缘部署及负载安全成亚太地区关注焦点
创建好后的模型,对Con2d, ConvTranspose2d ,以及归一化BatchNorm2d函数中的变量进行初始化