【leetcode】1161. Maximum Level Sum of a Binary Tree
题目如下:
Given the
rootof a binary tree, the level of its root is1, the level of its children is2, and so on.Return the smallest level
Xsuch that the sum of all the values of nodes at levelXis 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
1and10^4.-10^5 <= node.val <= 10^5
解题思路:没什么好说的,依次计算出每一层的值,求出最大即可。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
dic = {}
def recursive(self,node,level):
self.dic[level] = self.dic.setdefault(level,0) + node.val
if node.left != None:
self.recursive(node.left,level+1)
if node.right != None:
self.recursive(node.right,level+1) def maxLevelSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.dic = {}
self.recursive(root,1)
max_val = 0
res = 0
for key,val in self.dic.iteritems():
if max_val < val:
max_val = val
res = key
return res
【leetcode】1161. Maximum Level Sum of a Binary Tree的更多相关文章
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 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】671. Second Minimum Node In a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
随机推荐
- 基于jquery的bootstrap在线文本编辑器插件Summernote 简单强大
Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...
- kafka多线程消费
建立kafka消费类ConsumerRunnable ,实现Runnable接口: import com.alibaba.fastjson.JSON; import com.alibaba.fastj ...
- C# 程序异常停止后,sqlite可能变成0kb……
解决办法就是即时备份数据库文件,启动时判断数据库文件是否为0kb,是则还原之
- IP地址相关运算(如VLSM,超网汇总)
1.根据IP地址+子网掩码算出IP地址所在的网段(网络号) 例子: IP地址192.168.10.33,子网掩码为:255.255.255.240 (/28) ,写出所在的网络号 1.得出子网的块大小 ...
- python基础--函数1
# 一,为什么使用函数 # 1,可以使代码的组织结构清晰,可读性好 # 2,遇到重复的问题可以直接调用函数 # 3,功能扩展时,可直接修改,而无需每处都进行修改. # 二,函数为何物 # 函数对程序员 ...
- [转帖]青岛uber偷拍设备
爱彼迎民宿路由器暗藏摄像头:官方回应已移除房源 https://www.cnbeta.com/articles/tech/844233.htm 罚款 就是搞笑啊.. 不过现在偷拍设备真多... 5月5 ...
- redis在微博与微信等互联网应用笔记
Redis实战redis在微博与微信等互联网应用实例讲解全集 1. 对象缓存 id user balance 1 john 1200 2 tom 3000 对于这种存储,redis可以使用mset o ...
- Linux菜狗入门(不停更新)
资料来源:<腾讯课堂> 1, 计算机硬件包括CPU,内存,硬盘,声卡等等 2, 没有安装操作系统的计算机,通常被称为裸机 如果想在裸机上运行自己所编写的程序,就必须用机器语言书写程序 如果 ...
- A - 卿学姐与公主(线段树+单点更新+区间极值)
A - 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- O-超大型LED显示屏
Input 输入包含不超过100组数据.每组数据第一行为”START hh:mm:ss”,表示比赛开始时刻为hh:mm:ss.最后一行为”END hh:mm:ss”,即比赛结束时刻.二者之间至少会有一 ...