Easy to understand Python code with comments
Intuition:\n Describe your first thoughts on how to solve this problem. \nIt is clear that we will have to move on same level, i.e. breadth-wise.\n\n---\n\n# Ap
# Intuition:\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt is clear that we will have to move on same level, i.e. breadth-wise.\n\n---\n\n# Approach:\n<!-- Describe your approach to solving the problem. -->\nThe approach is very simple:\n1. Make an empty list to store sum of values of all the nodes at level i+1, i.e. at index 0, level_sum of level 1 will be stored, at i = 1, level 2, ... .\n2. Travel to each node of same level (I\'ll tell you later how to do so).\n3. Whilst, keep on updating current_level_sum (initially 0) by adding node.val of the current node.\n4. When all the nodes of a level are travelled, a append level_sum in sum_list.\n5. When all levels are over, return index+1 of max() of sum_list\n\n# How to travel level-wise:\nApproach is to store nodes of same level in a queue (first in, first out) (here, implemented using list)\n- *current_level* is a list (queue) in which we store all nodes of current level.\n- *next_level* is another list (queue) in which we store all nodes of next level.\n1. We start at level 1 which as only root node, hence initialize *current_level* containing only root\n2. Start looping until *current_level* is found to be empty\n    1. Initialize *current_level_sum* = 0 and another list (queue) *next_level* = [] in which we will store nodes of next level\n    2. Now, start looping again on *current_level*, till no more nodes are left to travel\n        - Take one node out from *current_level* as *node*\n        - Update *current_level_sum* by adding *node.val*\n        - Append *node.left* in *next_level*, if left is not None\n        - Append *node.right* in *next_level*, if right is not None\n    3. Store *current_level_sum* in *sum_list* by appending it\n    4. Lastly, change the level, i.e., replace *current_level* with *next_level*\n3. Almost done, now return the level number (index+1) of maximum value of *sum_list*\n4. Smile!\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        sum_list = [] # final list that contains sum of each level\n        current_level = [root] # queue that stores all nodes of current level, initialized with level 1, i.e. only root node\n        \n        while current_level: # loop block executes when no. of nodes in current level > 0\n            current_level_sum = 0 # variable to store sum of node.val for each node in current level\n            next_level = [] # another queue to store nodes of next level\n            \n            while current_level: # loop block executes until all nodes of current level is travelled\n                node = current_level.pop(0) # taking out first node from current level queue\n                current_level_sum += node.val # adding value of current node to level sum\n                if node.left: # conditional block will execute when left child of current node exists\n                    next_level.append(node.left) # left child of current node saved at the end of next level queue\n                if node.right: # conditional block will execute when right child of current node exists\n                    next_level.append(node.right) # right child of current node saved at the end of next level queue\n            \n            sum_list.append(current_level_sum) # sum of values of all nodes of current level is saved\n            current_level = next_level[:] # deep copy (full slice) of next level into current level variable\n        \n        return sum_list.index(max(sum_list)) + 1 # returning level number of maximum level sum\n```\n\n```python []\nif was_helpful(this_explaination):\n    upvote_count += 1\n```\n
['Python3']