当前位置:网站首页>365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
2022-07-31 12:28: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;
}
};
边栏推荐
- ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
- 关于IDEA开发工具的介绍
- DCM middleware family welcomes a new member
- JVS设置不同应用的登录时效时间
- Getting started with jmeter performance testing steps (performance testing tool jmeter)
- How does the SAP ABAP OData service support the $filter (filter) operation trial version
- 在 Excel 里使用 ODBC 读取 SAP BTP 平台上 CDS view 的数据
- LeetCode - 025. 链表中的两数相加
- mysql根据多字段分组——group by带两个或多个参数
- A40i/T3 uboot启动时对PMU部分初始化
猜你喜欢

消息队列面试题(2022最新整理)

ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI

Exploring Plain Vision Transformer Backbones for Object Detection Paper Reading Notes

kubernetes之服务发现

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

JVS轻应用的组成与配置

串的基本概念与操作

A Week of Wonderful Content Sharing (Issue 14)

三六零与公安部三所发布报告:关基设施保护成为网络安全博弈关键

JVS开发套件产品定位
随机推荐
0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
【Shader】Shader官方示例[通俗易懂]
JS列表数据通过递归实现树形结构
Service discovery of kubernetes
连续变量离散化教程
ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动
Structural controllability of switched linear systems with symmetry constraints
WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)
Use docker to build mysql master-slave
栈和队列的基本概念
jmeter性能测试步骤入门(性能测试工具jmeter)
函数递归1.0
最长算术(暑假每日一题 11)
ipv4和ipv6对比(IPV4)
JVM 运行时数据区与JMM 内存模型详解
CWE4.8 -- 2022年危害最大的25种软件安全问题
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
函数的参数
Use Excel to read data exposed by SAP ABAP CDS View through ODBC