Breadth-First Search Simple Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nThe approach was to use bfs to calculate sum of each level.\n# Approach\n Describe your
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe approach was to use bfs to calculate sum of each level.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst you want to have a queue to store the nodes of each level as you go down. Then as you go through the nodes, you add that to a sum variable in this case it was l_sum. After which it will be used to compare against the m_sum which is the max sum found. If it is then you can store the level index which is called idx in the code below. Then at the end it will return m_i which is the max sum index.\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\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        m_sum,m_i,idx = root.val,1,1\n        q = deque()\n        q.append(root)\n        while q:\n            l_sum = 0\n            for i in range(len(q)):\n                node = q.popleft()\n                if node:\n                    l_sum+=node.val\n                    if node.left:\n                        q.append(node.left)\n                    if node.right:\n                        q.append(node.right)\n            if l_sum > m_sum:\n                m_sum = l_sum\n                m_i = idx\n            idx+=1\n        return m_i\n```
['Tree', 'Breadth-First Search', 'Binary Tree', 'Python3']