当前位置:网站首页>365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
2022-07-31 12:32:00 【ShowM3TheCode】
1161. 最大层内元素和
代码实现(自解)
/** * 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) {
int maxSum = INT_MIN, maxLevel = 0;
queue<TreeNode*> _queue;
int curSum = 0, curLevel = 0, sz = 0;
TreeNode* curNode = NULL;
_queue.push(root);
while (!_queue.empty()) {
sz = _queue.size();
curSum = 0;
curLevel++;
while (sz--) {
curNode = _queue.front();
_queue.pop();
curSum += curNode->val;
if (curNode->left) _queue.push(curNode->left);
if (curNode->right) _queue.push(curNode->right);
}
if (curSum > maxSum) {
maxSum = curSum;
maxLevel = curLevel;
}
}
return maxLevel;
}
};
边栏推荐
- 双非一本进字节了!!纯干货分享
- 行业案例 | 全面防护 赛宁助力能源工控安全建设
- Use IN List Population in Your JDBC Application to Avoid Cursor Cache Contention Issues
- Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记
- 基于姿态估计的护具佩戴检测与动作识别
- Getting started with jmeter performance testing steps (performance testing tool jmeter)
- 阿里三面:MQ 消息丢失、重复、积压问题,怎么解决?
- AMBA APB学习记录(AMBA 3/4)
- CameraToolUnity中两种摄像机的两种观察控制方式
- MySQL面试八股文(2022最新整理)
猜你喜欢
随机推荐
基于稳态视觉诱发电位和注意力脑电的混合脑机接口系统
ipv4和ipv6对比(IPV4)
The 2nd activity of the TOGAF10 Standard Reading Club continues wonderfully, and the highlights will be reviewed!
CWE4.8 -- 2022年危害最大的25种软件安全问题
JVS设置不同应用的登录时效时间
log4j2的使用
How does the SAP ABAP OData service support the $filter (filter) operation trial version
【OpenCV】-边缘检测汇总示例
函数的参数
线性表的基本概念
JS列表数据通过递归实现树形结构
NameNode故障处理的两种方法
这款悄然崛起的国产API接口管理工具,你一定要晓得
Markdown编辑器语法
快速学完数据库管理
Using SQL Server FOR XML and FOR JSON syntax on other RDBMSs with jOOQ
最长算术(暑假每日一题 11)
Two methods of NameNode failure handling
Caused by: 类找不到: org.apache.flink.table.planner.delegation.ParserFactory或者ExecutorFactory
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解