Construct Binary Tree From Preorder And Inorder Traversal

To solve this coding challenge, you need to construct a binary tree from given preorder and inorder traversal lists. Here's a detailed explanation and pseudocode to help you understand how you can accomplish this task.

Explanation

Preorder Traversal

In a preorder traversal, you visit nodes in the following order:
  1. Root
  2. Left Subtree
  3. Right Subtree
  4. For example, given
    [3, 9, 20, 15, 7]
    :
  5. The first element
    3
    is the root.
  6. The subtree rooted at
    3
    will have a root of
    9
    (following nodes as
    20, 15, 7
    ).
  7. Inorder Traversal

    In an inorder traversal, you visit nodes in the following order:
  8. Left Subtree
  9. Root
  10. Right Subtree
  11. For example, given
    [9, 3, 15, 20, 7]
    :
  12. Nodes to the left of
    3
    form the left subtree
    (9)
    .
  13. Nodes to the right of
    3
    form the right subtree
    (15, 20, 7)
    .
  14. Steps to Solve the Challenge

  15. Identify the root from the first element of the preorder list.
  16. Find the root's index in the inorder list, which helps in separating the left and right subtrees.
  17. Recursively perform the above steps to construct left and right subtrees.
  18. Detailed Steps in Pseudocode

    To clarify these steps, we break down the process into small and very explicit steps.
  19. Base Condition : If the inorder list is empty, return
    None
    (no more nodes to process).
  20. Root Identification :
    • Remove the first element from the preorder list; this is the current root.
    • Find this root’s index in the inorder list.
  21. Subtree Construction :
    • Elements to the left of the root in the inorder list form the left subtree.
    • Elements to the right form the right subtree.
  22. Recursive Construction :
    • Recursively build the left subtree by passing the corresponding segments of preorder and inorder lists.
    • Recursively build the right subtree similarly.

Pseudocode

                                            
// Function to build the tree from preorder and inorder traversal lists
function buildTree(preorder, inorder):
    // Base case: if inorder list is empty, return None
    if inorder is empty:
        return None

    // Get the root value from the preorder list (first element)
    root_value = preorder.pop(0)

    // Find the index of the root in the inorder list
    root_index = findIndex(inorder, root_value)

    // Create the root node
    root_node = TreeNode(root_value)

    // Build the left subtree by taking elements before root_index in inorder
    root_node.left = buildTree(preorder, inorder[0:root_index])
    
    // Build the right subtree by taking elements after root_index in inorder
    root_node.right = buildTree(preorder, inorder[root_index + 1:end])

    // Return the constructed root_node
    return root_node

// Helper function to find the index of a value in a list
function findIndex(list, value):
    for i from 0 to length(list) - 1:
        if list[i] == value:
            return i
    return -1

                                        
In the pseudocode:
  • buildTree
    is a recursive function that constructs the binary tree.
  • preorder.pop(0)
    removes and returns the first element of the preorder list, giving us the current root node.
  • findIndex
    is a helper function to locate the index of a value in the list. This is necessary to determine which elements belong to the left and right subtrees in the inorder sequence.
  • When we make recursive calls to
    buildTree
    , the portions of the lists corresponding to the left and right subtrees are passed by slicing the inorder list around the root index.
To summarize, this methodology leverages the properties of preorder and inorder traversals to systematically build the tree by recursively partitioning the lists into left and right subtrees and creating nodes accordingly. This detailed and systematic approach ensures a correct tree reconstruction process.