题目如下:

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

代码如下:

# 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
05-28 00:31