1161. Maximum Level Sum of a Binary Tree
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree
Python
from collections import defaultdict
class Solution:
def maxLevelSum(self, root: Optional[TreeNode]) -> int:
mapper = defaultdict(int)
def dfs(node, level):
if not node:
return
mapper[level] += node.val
dfs(node.left, level+1)
dfs(node.right, level+1)
dfs(root, 1)
reverse_map = defaultdict(list)
for key, value in mapper.items():
reverse_map[value].append(key)
return min(reverse_map[max(mapper.values())])