当前位置:网站首页>LeetCode·每日一题·1161.最大层内元素和·层次遍历
LeetCode·每日一题·1161.最大层内元素和·层次遍历
2022-07-31 13:22:00 【小迅想变强】
链接:https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/solution/by-xun-ge-v-k02i/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目

示例

思路
解题思路
对于树的相关问题使用递归基本上都可以解决
对于本题我们需要寻找层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个。
那么首先需要对每一层的元素进行遍历求和,对于树的每一层元素遍历,无疑使用层次遍历最为方便,对于层次遍历可以用递归也可以使用迭代,对于层次遍历迭代更方便理解,这里使用迭代实现层次遍历
具体实现
树的层次遍历需要使用队列保存每一层的元素,我们遍历树,将树的每一层元素存入队列,然后按每层弹出队列中元素,同时保存每层元素的和,并且判断弹出元素是否存在子节点,存在子节点时存入队列,每层弹出后保存最大值和对应层数
代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxLevelSum(struct TreeNode* root){
struct TreeNode * ans[10000];//队列
int left = 0;
int right = 0;
ans[right++] = root;//头结点入队
int max = INT_MIN;//最大值层和
int storey = 1;//当前位于第几层
int s = -1;//最大值位于第几层
while(left < right)//层次遍历
{
int sum = 0;//当前层元素和
int r = right;
while(left < r)//按层弹出元素
{
sum += ans[left]->val;
if(ans[left]->left)//弹出元素判断是否存在子元素
{
ans[right++] = ans[left]->left;
}
if(ans[left]->right)
{
ans[right++] = ans[left]->right;
}
left++;
}
if(sum > max)//保存最大值层
{
max = sum;
s = storey;
}
storey++;
}
return s;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/solution/by-xun-ge-v-k02i/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。时间空间复杂度

边栏推荐
- 如何使用StarUML画类图[通俗易懂]
- Samba 远程命令执行漏洞(CVE-2017-7494)
- 为什么 wireguard-go 高尚而 boringtun 孬种
- Reasons and solutions for Invalid bound statement (not found)
- 365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
- The latest complete code: Incremental training using the word2vec pre-training model (two loading methods corresponding to two saving methods) applicable to various versions of gensim
- C#控件StatusStrip使用
- C# List Usage List Introduction
- C# control ListView usage
- 操作符详解
猜你喜欢

docker部署完mysql无法连接

golang-gin - graceful restart

Grab the tail of gold, silver and silver, unlock the programmer interview "Artifact of Brushing Questions"

Samba 远程命令执行漏洞(CVE-2017-7494)

C#获得网卡信息 NetworkInterface IPInterfaceProperties

网络层重点协议——IP协议

networkx绘制度分布

ECCV 2022 | 机器人的交互感知与物体操作

csdn发文助手问题

报错:npm ERR code EPERM
随机推荐
文本相似度计算(中英文)详解实战
STM32的CAN过滤器
The cluster of safe mode
Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
mysql8, starttime的下一个值作为endtime的上一个值?
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.
Centos7 install mysql5.7 steps (graphical version)
docker部署完mysql无法连接
TensorRT安装及使用教程「建议收藏」
深入浅出边缘云 | 4. 生命周期管理
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
如何使用StarUML画类图[通俗易懂]
AI cocoa AI frontier introduction (7.31)
networkx绘制度分布
PyQt5 rapid development and actual combat 10.1 Get city weather forecast
P5019 [NOIP2018 提高组] 铺设道路
IDEA版Postman插件Restful Fast Request,细节到位,功能好用
Network layer key protocol - IP protocol
电脑重要文件很多,如何备份比较安全?