当前位置:网站首页>LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
2022-06-24 06:44:00 【HERODING23】
解题思路:
BFS遍历每一层,统计每一层的最大值放入数组中,代码如下:
/** * 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;
}
};
边栏推荐
- Description of module data serial number positioning area code positioning refers to GBK code
- 二分专题训练
- More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?
- 20 not to be missed ES6 tips
- [Lua language from bronze to king] Part 2: development environment construction +3 editor usage examples
- [WUSTCTF2020]爬
- [机缘参悟-29]:鬼谷子-内揵篇-与上司交往的五种层次
- Learning to use BACnet gateway of building control system is not so difficult
- 什么是CC攻击?如何判断网站是否被CC攻击? CC攻击怎么防御?
- 6000多万铲屎官,捧得出一个国产主粮的春天吗?
猜你喜欢
bjdctf_2020_babystack
buuctf misc [UTCTF2020]docx
MaxCompute远程连接,上传、下载数据文件操作
How to connect the Bluetooth headset to the computer and how to connect the win10 computer to the Bluetooth headset
图形技术之坐标转换
【Vulhub靶场】】zabbix-SQL注入(CVE-2016-10134)漏洞复现
[WordPress website] 5 Set code highlight
(cve-2020-11978) command injection vulnerability recurrence in airflow DAG [vulhub range]
Prefix and topic training
Description of module data serial number positioning area code positioning refers to GBK code
随机推荐
How to distinguish PAAS, IAAs and SaaS?
More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?
buuctf misc 从娃娃抓起
Analog display of the module taking software verifies the correctness of the module taking data, and reversely converts the bin file of the lattice array to display
[image fusion] multi focus and multi spectral image fusion based on pixel saliency and wavelet transform with matlab code
[从零开始学习FPGA编程-42]:视野篇 - 后摩尔时代”芯片设计的技术演进-1-现状
What is the mentality of spot gold worth learning from
[机缘参悟-29]:鬼谷子-内揵篇-与上司交往的五种层次
2、 What is the principle of layer 3 and 4 switching technology? Recommended collection!
[Lua language from bronze to king] Part 2: development environment construction +3 editor usage examples
二分专题训练
[WUSTCTF2020]alison_likes_jojo
The initial user names and passwords of Huawei devices are a large collection that engineers involved in Huawei business should keep in mind and collect!
Obtain the package name, application name, icon, etc. of the uninstalled APK through packagemanager. There is a small message
第三方软件测试公司如何选择?2022国内软件测试机构排名
get_started_3dsctf_2016
jarvisoj_level2
New ways to play web security [6] preventing repeated use of graphic verification codes
Only two lines are displayed, and the excess part is displayed with Ellipsis
What is automated testing? What software projects are suitable for automated testing?