当前位置:网站首页>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;
}
};
边栏推荐
- Performance Optimization - Animation Optimization Notes
- Six Stones Programming: Problems must be faced, methods must be skillful, and functions that cannot be done well must be solved
- Multithreading Case - Timer
- 树和二叉树的转换
- 制售假劣农资、非法占用耕地……公安部公布十起危害粮食生产安全犯罪典型案例
- 微服务系统架构的演变
- 牛客刷SQL--6
- Pytorch —— 分布式模型训练
- JMP Pro 16.0 software installation package download and installation tutorial
- Programmer's Romantic Tanabata
猜你喜欢
final关键字的作用 final和基本类型、引用类型
什么是元编程
荣信文化通过注册:年营收3.8亿 王艺桦夫妇为实控人
全球都热炸了,谷歌服务器已经崩掉了
龙口联合化学通过注册:年营收5.5亿 李秀梅控制92.5%股权
安培龙IPO过会:年营收5亿 同创伟业与中移创新是股东
微服务系统架构的演变
软件测试之发现和解决bug
快速理解拉格朗日乘子法
Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
随机推荐
AD单片机九齐单片机NY8B062D SOP16九齐
【5GC】5G网络切片与5G QoS的区别?
NFV迈向云原生时代:Network Service Mesh项目介绍
A Beginner's Guide to Performance Testing
DaemonSet of kubernetes and rolling update
【2022蓝帽杯】file_session && 浅入opcode
经纬信息IPO过会:年营收3.5亿 叶肖华控制46.3%股权
PIR人体感应AC系列感应器投光灯人体感应开关等应用定制方案
AtCoder Beginner Contest 261 D - Flipping and Bonus
Istio Pilot代码深度解析
树和二叉树的转换
论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers
我寻找的方向
mysql的基本使用
多线程案例——阻塞式队列
荣信文化通过注册:年营收3.8亿 王艺桦夫妇为实控人
【每日一题】1331. 数组序号转换
论文详读《基于改进 LeNet-5 模型的手写体中文识别》,未完待补充
tensorflow2.0手写数字识别(tensorflow手写体识别)
十九届浙大城院程序设计竞赛 F.Sum of Numerators(数学/找规律)