题目如下:

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

解题思路:没什么好说的,依次计算出每一层的值,求出最大即可。

代码如下:

# 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的更多相关文章

  1. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

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

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

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

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

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

  4. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

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

  5. 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)

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

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

  7. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  8. 【leetcode】1186. Maximum Subarray Sum with One Deletion

    题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...

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

随机推荐

  1. hibernate+spring mvc,解决hibernate对象懒加载,json序列化失败

    在使用spring MVC时,@ResponseBody 注解的方法返回一个有懒加载对象的时候出现了异常,以登录为例: @RequestMapping("login") @Resp ...

  2. Python Module_subprocess_子进程(程序调用)

    目录 目录 前言 软件环境 认识subprocess Popen Constructor构造函数 Class Popen的参数 args 调用程序 调用Shell指令 stdinstdoutstder ...

  3. expression,statement,definition ,identifier(symbol) ,literal(字面量) 术语

    expression: an expression evaluates to a value only statement: a statement containing executable cod ...

  4. 禁止SYS和SYSTEM用户远程登录Oracle oracle的sys和system默认密码

    alter system set remote_login_passwordfile=none scope=spfile; system默认:manager sys默认:change_on_insta ...

  5. GCC 使用-C语言编译过程

    任何一种高级语言,要想在机器上执行,必须翻译为机器能读懂的机器语言.编译器就相当于翻译官,将高级语言翻译为机器语言. GCC 最初只用了编译 C 语言程序,全称是 GNU C Compiler.后来扩 ...

  6. 安装docker-下载加速、失败、成功安装

    前提:已装VMware虚拟机和Centos系统(具体安装包和过程可以百度) 先看这里:非root身份登录系统需要在下面的命令前加“sudo ”(sudo:代表给权限,用root登录则不需要输入) 一. ...

  7. git关联github远程仓库的问题

    git关联github远程仓库的时候,报fatal: remote origin already exists. 导致这个问题原因可能是之前关联的时候关联错了,再次关联就不行了. 解决办法是: 1.将 ...

  8. HashMap中确定数组位置为什么要用hash进行扰动

    HashMap数据存储的过程先根据key获得hash值,通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存 ...

  9. steps 步骤条、时间轴

    steps 步骤条.时间轴:http://www.fxss5201.cn/project/plugin/steps/1.0/ Github地址:https://github.com/fxss5201/ ...

  10. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...