beats 100% users using breadth first search || python
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks for the level of a binary tree where the sum of node values is the lar
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks for the level of a binary tree where the sum of node values is the largest. This can be solved using level-order traversal (BFS), where you traverse the tree level by level and sum up the values at each level.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Level-order Traversal (BFS):\n    - We use a queue (deque) to traverse the tree level by level.\n    - For each level, we pop nodes from the queue, sum up their values, and append their children (left and right) back to the queue for the next level.\n2. Track the sum:\n    - For each level, we calculate the sum of the node values and store it.\n3. Find the maximum sum:\n    - After calculating the sum of all levels, we find the level with the maximum sum and return its 1-based index.\n\n# Complexity\n**Time complexity:**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Each node in the tree is visited once, so the time complexity is O(n), where n is the number of nodes in the tree.\n\n**Space complexity:**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- The space complexity depends on the number of nodes stored in the queue at the largest level of the tree. In the worst case (a full binary tree), the maximum number of nodes at the last level is about n/2, so the space complexity is O(n).\n\n# Code\n```python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def maxLevelSum(self, root: Optional[TreeNode]) -> int:\n        if root is None:\n            return []\n        ans=[]\n        q=deque([root])\n        while q:\n            currLevel=[]\n            for a in range(len(q)):\n                node=q.popleft()\n                currLevel.append(node.val)\n                if node.left is not None:\n                    q.append(node.left)\n                if node.right is not None:\n                    q.append(node.right)\n            ans.append(currLevel)\n        res=[]\n        for i in ans:\n            res.append(sum(i))\n        return res.index(max(res))+1\n```\n# **PLEASE UPVOTE THIS CODE !!**
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'Python', 'Python3']