当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间空间复杂度
边栏推荐
- Reasons and solutions for Invalid bound statement (not found)
- NameNode (NN) and SecondaryNameNode (2NN) working mechanism
- csdn发文助手问题
- The importance of strategic offensive capability is much higher than strategic defensive capability
- C# control ToolStripProgressBar usage
- golang-gin-pprof-使用以及安全问题
- Sliding window method to segment data
- Architecture Camp | Module 8
- golang中使用泛型
- Save and load numpy matrices and vectors, and use the saved vectors for similarity calculation
猜你喜欢
机器学习模型验证:被低估的重要一环
ECCV 2022 | Robotic Interaction Perception and Object Manipulation
C# control ListView usage
Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
golang-gin-优雅重启
3.爬虫之Scrapy框架1安装与使用
C# using NumericUpDown control
C#控件 ToolStripProgressBar 用法
How IDEA runs web programs
随机推荐
Save and load numpy matrices and vectors, and use the saved vectors for similarity calculation
Selenium自动化测试之Selenium IDE
Samba 远程命令执行漏洞(CVE-2017-7494)
golang-gin-优雅重启
CodeIgniter 打开错误日志
go中select语句
聊聊 SAP 产品 UI 上的消息显示机制
networkx绘制度分布
报错IDEA Terminated with exit code 1
爱可可AI前沿推介(7.31)
Error: npm ERR code EPERM
A detailed explanation of the usage of Async and Await in C#
matlab as(assert dominance)
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
Text similarity calculation (Chinese and English) detailed explanation of actual combat
为什么 wireguard-go 高尚而 boringtun 孬种
C# using NumericUpDown control
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
Usage of += in C#
Google Chrome(谷歌浏览器)安装使用