当前位置:网站首页>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]
边栏推荐
- Which platform is a good place to open a futures account? Is it safe to open an online futures account?
- MySQL series: overview of the overall architecture
- golang 重要知识:atomic 原子操作
- Moher College - manual SQL injection vulnerability test (MySQL database)
- 医学影像分割的网站
- 图片读取:Image.open(ImgPath)
- freemark 使用ftl文件 生成word
- 嵌入式软件架构设计-程序分层
- How can genetic testing help patients fight disease?
- 基因检测,如何帮助患者对抗疾病?
猜你喜欢

The "shoulder" of sales and service in the heavy truck industry, Linyi Guangshun deep ploughing product life cycle service
Sorting out and summarizing the handling schemes for the three major exceptions of redis cache

Big factory Architect: how to draw a grand business map?

电子学会图形化一级编程题解析:猫捉老鼠
Solution to the problem that MySQL cannot be started in xampp

基因检测,如何帮助患者对抗疾病?
![[普通物理] 光的衍射](/img/1a/20dbd15e0c8c91a3e59753b2f6797a.png)
[普通物理] 光的衍射

stylegan1: a style-based henerator architecture for gemerative adversarial networks

30. concatenate substrings of all words

mysql 系列:总体架构概述
随机推荐
System design and analysis - Technical Report - a solution for regularly clearing verification code
Memory Consistency and Cache Coherence —— 内存一致性
Introduction to the push function in JS
ABP框架之——数据访问基础架构(下)
Pop() element in JS
Six programming insights in these five years!
TCP协议三次握手和四次挥手抓包分析
Detailed explanation of MQ message oriented middleware theory
Diffraction of light
Detailed steps for MySQL dual master configuration
JS里的数组
Gartner's latest report: development of low code application development platform in China
glibc nptl库pthread_mutex_lock和pthread_mutex_unlock浅析
The meaning of FPGA abbreviations and words in engineering field
golang 重要知识:RWMutex 读写锁分析
【无标题】激光焊接在医疗中的应用
pytorch:模型的保存与导出
英特尔Arc A380显卡消息汇总:跑分亮眼驱动拉胯 入门性价产品亟待优化
C. Add One--Divide by Zero 2021 and Codeforces Round #714 (Div. 2)
golang 重要知识:mutex