当前位置:网站首页>LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
2022-07-31 02:10:00 【HERODING23】

解题思路
套用BFSThe template can easily solve this problem,首先定义队列,Used to store nodes in layer order,Then iterate through each layer in the queue,统计总和,And update the number of layers where the maximum sum is located,so on until the queue is empty,It also means that the binary tree has been traversed,代码如下:
代码
/** * 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:
int maxLevelSum(TreeNode* root) {
queue<TreeNode*> q;
int maxNum = INT_MIN;
int layer = 1;
int cur = 0;
q.emplace(root);
while(!q.empty()) {
cur ++;
int n = q.size();
int total = 0;
for(int i = 0; i < n; i ++) {
TreeNode* temp = q.front();
total += temp->val;
if(temp->left != nullptr) {
q.emplace(temp->left);
}
if(temp->right != nullptr) {
q.emplace(temp->right);
}
q.pop();
}
if(total > maxNum) {
maxNum = total;
layer = cur;
}
}
return layer;
}
};
边栏推荐
- 成为比开发硬气的测试人,我都经历了什么?
- The PC side determines the type of browser currently in use
- Layer 2 broadcast storm (cause + judgment + solution)
- coldfusion8 background scheduled tasks take shell
- Validate XML documents
- Force buckled brush the stairs (7/30)
- 最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
- Drools basic introduction, introductory case, basic syntax
- What does a software test report contain?
- The real CTO is a technical person who understands products
猜你喜欢

【Map与Set】之LeetCode&牛客练习

Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?

The final exam first year course

leetcode-1161:最大层内元素和

pycharm cannot run after renaming (error: can't open file...No such file or directory)

934. The Shortest Bridge

Fiddler captures packets to simulate weak network environment testing

基于FPGA的售货机

类似 MS Project 的项目管理工具有哪些

There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
随机推荐
Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
CV-Model [3]: MobileNet v2
[1154]如何将字符串转换为datetime
Is there a way to earn 300 yuan a day by doing a side business?
GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
[1153]mysql中between的边界范围
力扣刷题之有效的正方形(每日一题7/29)
1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance
Crawler text data cleaning
项目开发软件目录结构规范
Drools WorkBench的简介与使用
Calculate S=a+aa+…+aa…a
mysql 索引
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
How to do a startup CTO?
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
Teach you how to configure Jenkins automated email notifications
MySQL installation tutorial (detailed, package teaching package~)
两个有序数组间相加和的Topk问题
AI中的数学思想