【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
题目描述
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Return the smallest level X such that the sum of all the values of nodes at level X is maximal.
Example 1:
Input: [1,7,0,7,-8,null,null]
Output: 2
Explanation:
Level 1 sum = 1.
Level 2 sum = 7 + 0 = 7.
Level 3 sum = 7 + -8 = -1.
So we return the level with the maximum sum which is level 2.
Note:
- The number of nodes in the given tree is between 1 and 10^4.
-10^5 <= node.val <= 10^5
题目大意
二叉树中一层的节点和最大的时候的最小层号。
解题方法
BFS
这个题考的是层次遍历,可以有两种做法,分别是BFS和DFS,类似题目是102. Binary Tree Level Order Traversal。这里使用的是BFS。
BFS需要一个队列存放当前层的所有叶子节点,然后出队列并且对这一层的所有叶子节点求和。
题目要求的是最大的和出现的最小层号,所以做个判断,如果当前层的和大于之前层,那么修改结果的层号。
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxLevelSum(TreeNode* root) {
int res_sum = INT_MIN;
int res_level = 1;
queue<TreeNode*> que;
que.push(root);
int level = 1;
while (!que.empty()) {
int size = que.size();
int level_sum = 0;
while (size --) {
TreeNode* cur = que.front(); que.pop();
if (!cur) continue;
level_sum += cur->val;
que.push(cur->left);
que.push(cur->right);
}
if (level_sum > res_sum) {
res_sum = level_sum;
res_level = level;
}
level ++;
}
return res_level;
}
};
日期
2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题
【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)的更多相关文章
- 【leetcode】1161. Maximum Level Sum of a Binary Tree
题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...
- leetcode1161 Maximum Level Sum of a Binary Tree
""" BFS遍历题,一遍AC Given the root of a binary tree, the level of its root is 1, the leve ...
- 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
随机推荐
- 【k8s】使用Terraform一键部署EKS集群
本文适用范文 使用AWS海外账号 对aws.terraform.k8s有一定的了解 新建一个独立的VPC Terraform简介 terraform是一个云端的资源编排工具,官方对自己的定位:Terr ...
- R shiny 小工具Windows本地打包部署
目录 服务器部署简介 windows打包部署 1. 部署基本框架 2.安装shiny脚本需要的依赖包 3.创建运行shiny的程序 [报错解决]无法定位程序输入点EXTPTE_PTR于动态链接库 将小 ...
- Mysql 预处理 PREPARE以及预处理的好处
Mysql 预处理 PREPARE以及预处理的好处 Mysql手册 预处理记载: 预制语句的SQL语法在以下情况下使用: · 在编代码前,您想要测试预制语句在您的应用程序中运行得如何.或者也许一个 ...
- Thinkphp5.1自动加载机制
Thinkphp5.1自动加载机制 自动加载机制 注册自动加载 引入静态自动加载映射文件,autoload_static.php 根据首字母前缀将不同的加载类归类-$prefixLengthsPsr4 ...
- perl substr
substr EXPR,OFFSET,LENGTH,REPLACEMENT substr EXPR,OFFSET,LENGTH substr EXPR,OFFSET Extracts a substr ...
- Redis集合解决大数据筛选
Redis集合:集合是什么,就是一堆确定的数据放在一起,数学上集合有交集.并集的概念,这个就可以用来做大数据的筛选功能. 以商品为例,假如商品有颜色和分类.价格区间等属性. 给所有统一颜色的商品放一个 ...
- 学习java的第六天
一.今日收获 1.开始了学习手册第二章的学习 2.了解了java里的常量与变量以及数据类型,与c语言的内容类似 二.今日难题 1.都是基础知识,没有什么难题 三.明日目标 1.继续学习java学习手册 ...
- 13. 搭建arm-linux-gcc交叉编译环境
1.下载工具并解压 下载路径 http://www.arm9.net/download.asp 将 arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz 拷贝到 Linux ...
- Camera、音频录制与Vitamio框架
一.Camera 1.概述 Android框架包含了各种相机哥相机功能的支持,是你可以在应用中捕获图像和视频. 在应用能使用设备上的相机之前,先想一想将来会如何使用此硬件: (1)Camera 应该 ...
- 解决CSV文件用Excel打开乱码问题
这篇文章适合有一定编码基础的人看,纯手动解决乱码问题请参见: 转码保存后,重新打开即可. 转码操作如下: 编辑器->另存为->ASCII码格式文件/UTF-8含BOM格式->保存. ...