Maximum Level Sum of a Binary Tree LeetCode solution in Python
1class Solution:2def maxLevelSum(self, root: Optional[TreeNode]) -> int:3global_max = float("-inf")4res = -15q = deque([root])6lvl = 17while q:8total = 09for _ in range(len(q)):10node = q.popleft()11if node.left:12q.append(node.left)13if node.right:14q.append(node.right)15total += node.val16if total > global_max:17res = lvl18global_max = total19lvl += 120return res