less than 1 minute read

1161. Maximum Level Sum of a Binary Tree (medium)

class Solution:
    def maxLevelSum(self, root: Optional[TreeNode]) -> int:
        q = deque([root])
        res = [-math.inf]
        while q:
            temp = deque([])
            num = 0
            while q:
                tar = q.popleft()
                num += tar.val
                if tar.left:
                    temp.append(tar.left)
                if tar.right:
                    temp.append(tar.right)
        
            res.append(num)
            q = temp
        
        index = 0
        for i in range(len(res)):
            if res[i] > res[index]:
                index = i
        return index