题目地址: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:

  1. The number of nodes in the given tree is between 1 and 10^4.
  2. -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++)的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  4. 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  5. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  6. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  7. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  8. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  9. 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

随机推荐

  1. Git 使用,本地项目上传到GitHub远程库

    Git 使用,本地项目上传到GitHub远程库 环境 GitHub账号 点此进入github官网 git客户端工具 点此进入git下载页 本地项目上传到 GitHub 在GitHub中创建一个仓库(远 ...

  2. 富集分析DAVID、Metascape、Enrichr、ClueGO

    前言 一般我们挑出一堆感兴趣的基因想临时看看它们的功能,需要做个富集分析.虽然公司买了最新版的数据库,如KEGG,但在集群跑下来嫌麻烦.这时网页在线或者本地化工具派上用场了. DAVID DAVID地 ...

  3. time 查看命令执行时间

    在命令执行完成之后就会打印出CPU的使用情况: real    0m5.064s      <== 实际使用时间(real time) user    0m0.020s     <== 用 ...

  4. cat的生产应用

    web日志文件的合并 cat one.log two.log >all.log sort -k 4 all.log   按照第四列进行时间排序

  5. 06 windows安装Python+Pycharm+Scrapy环境

    windows安装Python+Pycharm+Scrapy环境 使用微信扫码关注微信公众号,并回复:"Python工具包",免费获取下载链接! 一.卸载python环境 卸载以下 ...

  6. Java中static关键字声明的静态内部类与非静态内部类的区别

    (1)内部静态类不需要有指向外部类的引用.但非静态内部类需要持有对外部类的引用.(2)非静态内部类能够访问外部类的静态和非静态成员.静态类不能访问外部类的非静态成员.他只能访问外部类的静态成员.(3) ...

  7. Java 监控基础 - 使用 JMX 监控和管理 Java 程序

    点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码网站 已经收录,有很多知识点和系列文章. 此篇文 ...

  8. [php代码审计] Typecho 1.1 -反序列化Cookie数据进行前台Getshell

    环境搭建 源码下载:https://github.com/typecho/typecho/archive/v1.1-15.5.12-beta.zip 下载后部署到web根目录,然后进行安装即可,其中注 ...

  9. 爬虫系列:连接网站与解析 HTML

    这篇文章是爬虫系列第三期,讲解使用 Python 连接到网站,并使用 BeautifulSoup 解析 HTML 页面. 在 Python 中我们使用 requests 库来访问目标网站,使用 Bea ...

  10. 【leetcode】43. Multiply Strings(大数相乘)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...