Construct Binary Tree From Preorder And Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal

You are given two integer arrays, preorder and inorder:

  • preorder represents the preorder traversal of a binary tree.
  • inorder represents the inorder traversal of the same tree.
  • Both arrays have the same length and contain unique values.

Your task is to reconstruct the binary tree using these traversals and return its root.

Construct Binary Tree From Preorder And Inorder Traversal Problem

Example 1:

Construct Binary Tree From Preorder And Inorder Traversal Example

Example 2:

  • Input: preorder = [1], inorder = [1]
  • Output: [1]

Constraints:

  • 1 <= inorder.length <= 1000.
  • inorder.length == preorder.length
  • -1000 <= preorder[i], inorder[i] <= 1000

Hints to Solve Construct Binary Tree From Preorder And Inorder Traversal

Recommendation for Time and Space Complexity:

Aim for a solution with O(n) time and O(n) space, where n is the total number of nodes in the tree.

Hint 1 :

The inorder traversal splits the tree into two parts: the left and right subtrees based on the root node. Can you determine the root’s position in the inorder array using the preorder array?

Hint 2 :

In preorder traversal, the first node is always the root. Use this property to start reconstructing the binary tree.

Hint 3 :

Once you identify the root node from the preorder array, find its position in the inorder array. While a linear search works, it results in O(n^2) complexity. Is there a faster way to do this?

Hint 4 :

Using a hash map allows you to find the index of any node in the inorder array in O(1) time. Think about how you can build and use this efficiently.

Hint 5:

Use Depth First Search (DFS) for tree construction. Track the current index in the preorder array globally and use two pointers (l and r) to define the inorder segment for the current subtree.

For each node, create it, find its position using the hash map, and recursively build the left and right subtrees.

There are mainly 3 approach to solve this problem:

  1. Depth First Search Method (DFS)
  2. Hash Map Method
  3. Depth First Search (Optimal)

1. Depth First Search Method

Use the preorder array to identify the root node and divide the inorder array into left and right subtrees. Recursively repeat this process to construct the entire binary tree.

  • Time complexity: O(n^2)
  • Space complexity: O(n^2)

Code

2. Hash Map Method

Build a hash map to store the index of each value in the inorder array for quick lookup. This avoids linear searches, improving efficiency while constructing the tree.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

3. Depth First Search (Optimal) Method

Combine DFS with a hash map for faster root lookups. Use a global pointer to track the current root index in the preorder array and efficiently split the inorder array into left and right subtrees.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

More Articles