当前位置:网站首页>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;
}
};
边栏推荐
- 订song餐系统
- Indoor real-time laser SLAM control method based on biological excitation neural network
- JVS应用中心
- SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
- PAT考试总结(考试心得)
- Fully Dynamically Constrained Robot Efficient Time-Optimal Trajectory Planning
- ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
- 基于姿态估计的护具佩戴检测与动作识别
- NPM 使用介绍
- SAP message TK 248 solved
猜你喜欢
TOGAF10标准读书会第2场活动精彩继续,高光时刻回顾!
golang八股文整理(持续搬运)
深度学习基本概念
想吃菌子,当然是自己上山找了
AMBA APB学习记录(AMBA 2.0)
The 2nd activity of the TOGAF10 Standard Reading Club continues wonderfully, and the highlights will be reviewed!
Architecture Camp | Module 8
Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
串的基本概念与操作
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。
随机推荐
Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
三相PWM整流器预测直接功率控制
Chrome开发自定义右键菜单实现快速跳转到指定页面
列表页优化思路
纷享销客罗旭对话元气森林黄晓枫:零售数字化的终点不是创新,而是数据
WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)
关于我放弃考研这件事儿
log4j2的使用
TOGAF10标准读书会第2场活动精彩继续,高光时刻回顾!
CWE4.8 -- The 25 most damaging software security issues in 2022
365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
跨境电商小知识之跨境电商物流定义以及方式讲解
docker部署完mysql无法连接
CentOS7 installation MySQL graphic detailed tutorial
alert(1) (haozi.me)靶场练习
栈和队列的基本概念
最近两个月谷歌 ad 掉的厉害
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。
Google Chrome(谷歌浏览器)安装使用
2022年最新重庆建筑安全员模拟题库及答案