当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

centos7安装mysql5.7步骤(图解版)

Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?

CWE4.8 -- 2022年危害最大的25种软件安全问题

深度学习基本概念

使用docker搭建mysql主从

荣耀手机参数写错,客服认为没错

线性表的基本概念

How does the SAP ABAP OData service support the $filter (filter) operation trial version

深圳某游戏研发公司每个工位都装监控,网友:堪比“坐牢”!

订song餐系统
随机推荐
亲测可用!!!WPF中遍历整个窗口的所有TextBox组件,对每个输入框做非空判断。
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
全动力学约束的机器人高效时间最优轨迹规划
sqlalchemy 判断一个array 类型的字段是否和一个array有至少一个一致的数据
立一个flag
给你一个大厂面试的机会,你能面试上吗?进来看看!
阿里三面:MQ 消息丢失、重复、积压问题,怎么解决?
CWE4.8 -- The 25 most damaging software security issues in 2022
深度学习基本概念
集群中增加数据节点与退役数据节点
Structural controllability of switched linear systems with symmetry constraints
TOGAF10标准读书会第2场活动精彩继续,高光时刻回顾!
Use docker to build mysql master-slave
小试牛刀—猜数字游戏
立方体IV(暑假每日一题 10)
Data Lake (19): SQL API reads Kafka data and writes it to Iceberg table in real time
双非一本进字节了!!纯干货分享
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
Cognitive-exercise rehabilitation medical robot application design
Double non-one into bytes!!Pure dry goods sharing