Binary Tree Level Order Traversal Ii

To solve this coding challenge, we need to perform a bottom-up level order traversal of a binary tree. This means that we retrieve and store node values starting from the leaves of the tree up to the root, while processing each level from left to right. Here is an elucidated approach:

Explanation

  1. Understand the Problem Statement :
    • Given a binary tree, we need to return the node values level by level, starting from the bottom-most level up to the root. Each level should be processed from left to right.
    • Tree nodes can contain values between -1000 and 1000, and the tree can have up to 2000 nodes.
  2. Approach :
    • BFS (Breadth-First Search) strategy is ideal for level-order traversal. We will use a queue to facilitate the BFS.
    • Start from the root node. If the tree is empty (i.e., root is
      null
      ), return an empty list.
    • Use a queue to track nodes level by level:
      • Enqueue the root node initially.
      • Process nodes level by level: for each node, enqueue its left child and right child (if they exist).
    • Store each level's values in a temporary list, and then store that list at the beginning of the result list. This way, the first processed level (root level) ends up being the last element in the result list.
  3. Detailed Steps in Pseudocode :
    • Initialize an empty queue and enqueue the root node.
    • Initialize an empty result list.
    • While the queue is not empty:
      • Record the number of elements currently in the queue (this represents the number of nodes at the current level).
      • Initialize a temporary list to store current level’s node values.
      • Dequeue each node, store its value, and enqueue its children (left and right).
      • After processing all nodes at the current level, insert the temporary list of node values at the beginning of the result list.
    • Finally, return the result list.
  4. Example Walkthrough :
    • Consider the example
      [3,9,20,null,null,15,7]
      .
    • Level 0 (root):
      [3]
    • Level 1:
      [9, 20]
    • Level 2:
      [15, 7]
    • Output Order (from bottom to top):
      [[15, 7], [9, 20], [3]]

Pseudocode Explanation

                                            
# Function to perform bottom-up level order traversal

Function levelOrderBottom(root):

    # If the root is null, return an empty list
    If root is null:
        return []

    # Initialize the result list which will store the final level order traversal bottom-up
    result = []
    
    # Initialize the queue with the root node
    queue = [root]
    
    # Loop until the queue is empty
    While queue is not empty:
        
        # Number of nodes at the current level
        size = length of queue
        
        # Temporary list to store node values at the current level
        level_values = []
        
        # Process all nodes at the current level
        For i in range from 0 to size - 1:
            # Dequeue the node from the front of the queue
            node = dequeue(queue)
            
            # Add the node's value to the current level's list
            level_values.append(node.value)
            
            # Enqueue the left child if it exists
            If node.left is not null:
                enqueue(queue, node.left)
                
            # Enqueue the right child if it exists
            If node.right is not null:
                enqueue(queue, node.right)
        
        # Insert the current level's list at the beginning of the result list
        insert result at index 0 with level_values
    
    # Return the final result
    return result

                                        
By deploying this methodical approach, you ensure that node values are processed level by level, and collected from the bottom-most level up to the root level. All nodes are managed within a queue to facilitate Breadth-First Search traversal, and the resultant levels are prepended to the result list to achieve the bottom-up ordering.