当前位置:网站首页>Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row
Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row
2022-06-24 08:01:00 【HERODING23】

Their thinking :
BFS Go through each layer , Count the maximum value of each layer and put it into the array , The code is as follows :
/** * Definition for a binary tree node. * 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:
vector<int> largestValues(TreeNode* root) {
if(root == nullptr) return {
};
queue<TreeNode*> q;
q.emplace(root);
vector<int> ans;
while(!q.empty()) {
int n = q.size();
int maxNum = INT_MIN;
for(int i = 0; i < n; i ++) {
TreeNode* node = q.front();
q.pop();
maxNum = max(maxNum, node->val);
if(node->left != nullptr) q.emplace(node->left);
if(node->right != nullptr) q.emplace(node->right);
}
ans.emplace_back(maxNum);
}
return ans;
}
};
边栏推荐
- Inline element, block element, inline block element
- From jsonpath and XPath to spl
- Chrono usage notes
- 火线,零线,地线,你知道这三根线的作用是什么吗?
- How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
- 开放合作,共赢未来 | 福昕鲲鹏加入金兰组织
- Moonwell Artemis现已上线Moonbeam Network
- 1-4metasploitable2介绍
- OpenGauss数据库在 CentOS 上的实践,配置篇
- 免费ICP域名备案查接口
猜你喜欢

Configure your own free Internet domain name with ngrok

语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)

Part 2: drawing a window

What kind of experience is it when the Institute earns 20000 yuan a month!

Vulnhub target: boredhackerblog_ CLOUD AV

Practice of opengauss database on CentOS, configuration

SCM stm32f103rb, BLDC DC motor controller design, schematic diagram, source code and circuit scheme

Case examples of corpus data processing (cases related to sentence retrieval)
![[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board](/img/10/6725b51120a6ae8b16d60f5b1ae904.jpg)
[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board

毕业两年月薪36k,说难也不难吧
随机推荐
auto使用示例
Hongmeng development IV
What kind of experience is it when the Institute earns 20000 yuan a month!
Leetcode 174 Dungeon games (June 23, 2022)
第 3 篇:绘制三角形
opencvsharp二值图像反色
POM configuration provided and test
『C语言』系统日期&时间
Domain environment importing Tencent cloud considerations
Ad-gcl:advantageous graph augmentation to improve graph contractual learning
Specify IP when calling feign interface
Any remarks
Free ICP domain name filing interface
Moonwell Artemis现已上线Moonbeam Network
免费ICP域名备案查接口
Exploration on Optimization of elastic expansion engineering
保留一位小数和保留两位小数
js实现查看一个数组对象中是否包含另一个数组对象中的值
The startup mode of cloudbase init is \Cloudbase init has hidden dangers
[run the script framework in Django and store the data in the database]