当前位置:网站首页>1161. 最大层内元素和
1161. 最大层内元素和
2022-08-01 14:05:00 【anieoo】
原题链接:1161. 最大层内元素和
solution:
/**
* 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) {
if(root == nullptr) return 0;
queue<TreeNode*> que;
que.push(root);
int i = 1;
int res = 0;
int maxs = INT_MIN;
while(!que.empty()) {
int size = que.size();
int sum = 0;
while(size--) {
auto t = que.front();
que.pop();
sum += t->val;
if(t->left) que.push(t->left);
if(t->right) que.push(t->right);
}
if(sum > maxs) {
maxs = sum;
res = i;
}
i++;
}
return res;
}
};边栏推荐
猜你喜欢
随机推荐
MBI5020 LED Driver
Istio投入生产的障碍以及如何解决这些问题
牛客刷SQL--5
iframe标签属性说明 详解[通俗易懂]
28uA待机8米距离低压保护单片机探头太阳能灯人体PIR定制方案
嵌入式开发:创建和使用可移植类型的7个技巧
【每日一题】952. 按公因数计算最大组件大小
魔众短链接系统 v3.9.0
DaemonSet of kubernetes and rolling update
2022-07-25 网工进阶(二十一)BGP-路由反射器、联盟、聚合
The problem that the column becomes indexed after pd groupby and the aggregation column has no column name
MCU开发是什么?国内MCU产业现状如何
Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3
使用open3d可视化3d人脸
Istio Pilot代码深度解析
微服务原生案例搭建
VINS-mono 论文解读:IMU预积分+Marg边缘化
分布式中的远程调用
Performance Optimization - Rendering Optimization Notes
高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能









