---
# System prepended metadata

title: 1161. Maximum Level Sum of a Binary Tree
tags: [Leetcode, Python]

---

# 1161. Maximum Level Sum of a Binary Tree
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
###### tags: `Python`,`Leetcode`

## Code!
``` python=
from collections import deque
class Solution:
    def maxLevelSum(self, root: Optional[TreeNode]) -> int:
        maxi, level, maxlevel = -float('inf'), 0, 0
        q = deque()
        q.append(root)
        while q :
            level += 1 
            summation = 0
            for i in range(len(q)):
                node = q.popleft()
                summation += node.val
                if node.left:
                    q.append(node.left)
                
                if node.right:
                    q.append(node.right)
            if maxi < summation:
                maxi, maxlevel = summation, level
        
        return maxlevel
```