当前位置:网站首页>LeetCode 1161.最大层内元素和:层序遍历
LeetCode 1161.最大层内元素和:层序遍历
2022-07-31 10:55:00 【Tisfy】
【LetMeFly】1161.最大层内元素和
力扣题目链接:https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/
给你一个二叉树的根节点 root
。设根节点位于二叉树的第 1
层,而根节点的子节点位于第 2
层,依此类推。
请返回层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个。
示例 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
提示:
- 树中的节点数在
[1, 104]
范围内 -105 <= Node.val <= 105
方法一:层序遍历 + 广搜BFS
有关二叉树的层序遍历,之前已经讲过,详细方法可参考 LeetCode 0107.二叉树的层序遍历II の 题解
本题,同样地,我们使用优先队列来对二叉树进行层序遍历。
- 用变量
maxSum
记录当前的单层最大和 - 用变量
ans
来记录取得maxSum
的最小层号 - 用变量
nowLayer
记录当前遍历到的层的层号
初始值maxSum
为int
范围内的最小值INT_MIN
,ans
取任意值即可,nowLayer
的值取1
。
在遍历到某一层时,用一个临时变量thisSum
统计这一层的节点值之和
如果这一层遍历结束后,thisSum
的值大于之前所记录的最大值maxSum
那么就更新maxSum
为thisSum
,并将ans
赋值为当前层号nowLayer
。
- 时间复杂度 O ( N ) O(N) O(N),其中 N N N是节点个数。
- 空间复杂度 O ( N 2 ) O(N2) O(N2),其中 N 2 N2 N2是节点最多的一层的节点数。
AC代码
C++
class Solution {
public:
int maxLevelSum(TreeNode* root) {
int maxSum = INT_MIN;
int ans = -1;
int nowLayer = 1;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int thisLayerNum = q.size(); // 这一层有几个元素
int thisSum = 0;
for (int i = 0; i < thisLayerNum; i++) {
TreeNode* thisNode = q.front();
q.pop();
thisSum += thisNode->val;
if (thisNode->left)
q.push(thisNode->left);
if (thisNode->right)
q.push(thisNode->right);
}
if (thisSum > maxSum) {
maxSum = thisSum;
ans = nowLayer;
}
nowLayer++;
}
return ans;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126082726
边栏推荐
- Curl 命令使用
- Redis-基础
- SQLServer2019安装(Windows)
- 【LeetCode】36.有效的数独
- Insertion and deletion of doubly linked list
- web安全入门-黑苹果MAC系统安装
- 【LeetCode】21. 合并两个有序链表
- 单点登录原理及实现方式
- 内网渗透学习(四)域横向移动——SMB和WMI服务利用
- "JUC Concurrent Programming - Advanced" 06 - Immutability of Shared Models (Design of Immutable Classes | Use of Immutable Classes | Flyweight Pattern)
猜你喜欢
使用内存映射加快PyTorch数据集的读取
KVM virtualization job
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
应用层基础 —— 认识URL
LeetCode二叉树系列——101.对称二叉树
医院管理系统数据库,课程设计,SQLserver,纯代码设计
SQL - Left join, Right join, Inner join
Sql optimization summary!detailed!(Required for the latest interview in 2021)
Usage of JOIN in MySQL
AtCoder—E - Σ[k=0..10^100]floor(X/10^k
随机推荐
基于Multisim的函数信号发生器–方波、三角波、正弦波[通俗易懂]
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
Sql优化总结!详细!(2021最新面试必问)
Usage of exists in sql
单点登录原理及实现方式
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
pycharm汉化教程(碧蓝幻想汉化插件安装)
darknet 训练分类网络
Windows系统Mysql8版本的安装教程
Redis的简单使用
结构化查询语言SQL-关系数据库标准语言
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
2022/7/30
Redis缓存面临的缓存击穿问题
Sql optimization summary!detailed!(Required for the latest interview in 2021)
Windows安装mysql详细步骤(通俗易懂,简单上手)
[Go Affair] See through Go's collections and slices at a glance
Inversion problem - key point
nodeJs--querystring模块