当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。时间空间复杂度

边栏推荐
- Buffer 与 拥塞控制
- 基于去噪自编码器的故障隔离与识别方法
- NameNode (NN) and SecondaryNameNode (2NN) working mechanism
- The importance of strategic offensive capability is much higher than strategic defensive capability
- Edge Cloud Explained in Simple Depth | 4. Lifecycle Management
- Centos7 install mysql5.7
- Adding data nodes and decommissioning data nodes in the cluster
- C#控件 ToolStripProgressBar 用法
- C#中+=的用法
- [CPU Design Practice] Simple Pipeline CPU Design
猜你喜欢

电脑重要文件很多,如何备份比较安全?

4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率

【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)

中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期

Architecture Camp | Module 8

Even if the image is missing in a large area, it can also be repaired realistically. The new model CM-GAN takes into account the global structure and texture details

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

Centos7 install mysql5.7

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your

网络协议及相关技术详解
随机推荐
NameNode (NN) and SecondaryNameNode (2NN) working mechanism
Centos7 install mysql5.7
PHP序列化:eval
golang-gin-pprof-使用以及安全问题
What should I do if selenium is reversed?
操作符详解
IDEA连接MySQL数据库并执行SQL查询操作
PyQt5 rapid development and actual combat 10.1 Get city weather forecast
C#高级--委托
Four ways to clear the float and its principle understanding
PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
selenium被反爬了怎么办?
Even if the image is missing in a large area, it can also be repaired realistically. The new model CM-GAN takes into account the global structure and texture details
CentOS7 - yum install mysql
IDEA版Postman插件Restful Fast Request,细节到位,功能好用
机器学习模型验证:被低估的重要一环
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%
How to quickly split and merge cell data in Excel
报错:npm ERR code EPERM
Detailed explanation of network protocols and related technologies