当前位置:网站首页>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]
边栏推荐
猜你喜欢

Redis集群操作的方法

服务器的部署及使用说明
![[MAE]Masked Autoencoders掩膜自编码器](/img/08/5ab2b0d5b81c723919046699bb6f6d.png)
[MAE]Masked Autoencoders掩膜自编码器

js的slice()和splice()

mysql 系列:存储引擎

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

这五年的6个编程感悟!

513. Find Bottom Left Tree Value

Three simple tips for accelerating yarn install

Important knowledge of golang: rwmutex read / write lock analysis
随机推荐
Top 10 purchase, sales and inventory software rankings!
matlab: 如何从一些数据里知道是由哪些数据相加得出一个已知数
Redis集群操作的方法
golang 重要知识:atomic 原子操作
139. word splitting
这五年的6个编程感悟!
Web篇_01 了解web开发
Nfnet: extension of NF RESNET without BN's 4096 super batch size training | 21 year paper
Important knowledge of golang: timer timer
139. 單詞拆分
一文看懂经典BUCK-BOOST负电压电路
5 minutes to quickly launch web applications and APIs (vercel)
glibc nptl库pthread_mutex_lock和pthread_mutex_unlock浅析
Usestate vs useref and usereducer: similarities, differences and use cases
现在我要买股票,怎么开户?手机开户安全么?
MIPI C-PHY协议你了解吗?手机高速接口之一
Convert JSON file of labelme to coco dataset format
Important knowledge of golang: mutex
电荷泵原理讲义,电压是怎么“泵”上去的?
freemark 使用ftl文件 生成word