-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path61.py
More file actions
26 lines (22 loc) · 683 Bytes
/
61.py
File metadata and controls
26 lines (22 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxLevelSum(self, root: Optional[TreeNode]) -> int:
cache = {}
res = []
def dfs(node, level):
if node is None:
return
v = cache.get(level, [])
cache[level] = v + [node.val]
dfs(node.left, level + 1)
dfs(node.right, level + 1)
dfs(root, 1)
for k, v in cache.items():
res.append((-sum(v), k))
res.sort()
return res[0][1]