当前位置:网站首页>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;
}
};
边栏推荐
- 基于opencv实现人脸检测
- The final exam first year course
- What are the project management tools like MS Project
- General introduction to the Unity interface
- 力扣刷题之有效的正方形(每日一题7/29)
- Intranet Infiltration - Privilege Escalation
- How to expose Prometheus metrics in go programs
- Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
- Unity界面总体介绍
- Are you still working hard on the limit of MySQL paging?
猜你喜欢
Observer mode (1)
怎样做好一个创业公司CTO?
mysql 视图
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
Detailed explanation of STP election (step + case)
How to do a startup CTO?
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
leetcode-952: Calculate max component size by common factor
Arbitrum 专访 | L2 Summer, 脱颖而出的 Arbitrum 为开发者带来了什么?
Drools规则属性,高级语法
随机推荐
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
Between two orderly array of additive and Topk problem
验证整数输入
Programmer's debriefing report/summary
How to expose Prometheus metrics in go programs
Software Testing Defect Reporting - Definition, Composition, Defect Lifecycle, Defect Tracking Post-Production Process, Defect Tracking Process, Purpose of Defect Tracking, Defect Management Tools
Tower of Hanoi problem
软件测试缺陷报告---定义,组成,缺陷的生命周期,缺陷跟踪产后处理流程,缺陷跟踪处理流程,缺陷跟踪的目的,缺陷管理工具
Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment
leetcode-399: division evaluation
充电效果模拟
怎样做好一个创业公司CTO?
Drools Rule Properties, Advanced Syntax
cudaMemcpy学习笔记
C语言小程序 -- 常见经典练习题
[Map and Set] LeetCode & Niu Ke exercise
MySQL的分页你还在使劲的limit?
Simple confession page
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
Can an inexperienced college graduate switch to software testing?my real case