当前位置:网站首页>1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
2022-07-31 17:48:00 【Water palace trifoliate brush diary】
题目描述
这是 LeetCode 上的 1161. 最大层内元素和 ,难度为 中等.
Tag : 「层序遍历」、「BFS」
给你一个二叉树的根节点 root
.设根节点位于二叉树的第 层,而根节点的子节点位于第 层,依此类推.
请返回层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个.
示例 1:
输入:root = [1,7,0,7,-8,null,null]
输出:2
解释:
第 1 层各元素之和为 1,
第 2 层各元素之和为 7 + 0 = 7,
第 3 层各元素之和为 7 + -8 = -1,
所以我们返回第 2 层的层号,它的层内元素之和最大.
示例 2:
输入:root = [989,null,10250,98693,-89388,null,null,null,-32127]
输出:2
提示:
树中的节点数在 范围内
层序遍历
根据题意,使用 BFS
You can perform layer-order traversal.
每次以「层」Expand the unit,Count the element sum of this layer,The maximum layer sum during maintenance processing,and layer depth.
Java 代码:
class Solution {
public int maxLevelSum(TreeNode root) {
Deque<TreeNode> d = new ArrayDeque<>();
int max = -0x3f3f3f3f, depth = 1, ans = 0;
d.addLast(root);
while (!d.isEmpty()) {
int sz = d.size(), cur = 0;
while (sz-- > 0) {
TreeNode t = d.pollFirst();
if (t.left != null) d.addLast(t.left);
if (t.right != null) d.addLast(t.right);
cur += t.val;
}
if (cur > max) {
max = cur; ans = depth;
}
depth++;
}
return ans;
}
}
TypeScript 代码:
function maxLevelSum(root: TreeNode | null): number {
const d: TreeNode[] = new Array<TreeNode>()
let he = 0, ta = 0
d[ta++] = root
let max = -0x3f3f3f3f, depth = 1, ans = 0
while (he < ta) {
let sz = ta - he, cur = 0
while (sz-- > 0) {
const t = d[he++]
if (t.left != null) d[ta++] = t.left
if (t.right != null) d[ta++] = t.right
cur += t.val
}
if (cur > max) {
max = cur; ans = depth
}
depth++
}
return ans
};
时间复杂度: 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.1161
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完.
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码.如果涉及通解还会相应的代码模板.
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode .
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解.
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- MySQL common statements
- Masterless replication system (1) - write DB when node fails
- [pytorch] pytorch automatic derivation, Tensor and Autograd
- MySQL---sort and pagination
- 这位985教授火了!当了10年博导,竟无一博士毕业!
- 你辛辛苦苦写的文章可能不是你的原创
- The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
- 2022 Android interview summary (with interview questions | source code | interview materials)
- [TypeScript]OOP
- LevelSequence源码分析
猜你喜欢
华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
Apache EventMesh 分布式事件驱动多运行时
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
联邦学习:联邦场景下的多源知识图谱嵌入
【网络通信三】研华网关Modbus服务设置
【pytorch】pytorch 自动求导、 Tensor 与 Autograd
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
中文编码的设置与action方法的返回值
Automated testing - web automation - first acquaintance with selenium
全平台GPU通用AI视频补帧超分教程
随机推荐
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
几款永久免费内网穿透,好用且简单(内网穿透教程)
最后写入胜利(丢弃并发写入)
架构师04-应用服务间加密设计和实践
Cache and Database Consistency Solutions
35 MySQL interview questions and diagrams, this is also easy to understand
MySQL---基本的select语句
Mariabackup implements incremental data backup for Mariadb 10.3
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
认识异常 (看完这篇你就懂了)
新型电信“套路”,我爸中招了!
Flink_CDC搭建及简单使用
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
京东按关键字搜索商品 API
你辛辛苦苦写的文章可能不是你的原创
IP协议从0到1
【源码解析】BeanFactory和FactoryBean
[TypeScript] OOP
无主复制系统(1)-节点故障时写DB