6. Construct Tree from Inorder & Preorder

The problem can be found at the following link: Question Link

Problem Description

Given two arrays representing the inorder and preorder traversals of a binary tree, construct the tree and return the root node of the constructed tree. Note: The output is written in postorder traversal.

Example 1:

Input:

inorder[]  = [1, 6, 8, 7]
preorder[] = [1, 6, 7, 8]

Output:

[8, 7, 6, 1]

Explanation:

The constructed binary tree’s postorder traversal is [8, 7, 6, 1].

Example 2:

Input:

Output:

Explanation:

The constructed binary tree’s postorder traversal is [3, 4, 1, 5, 2, 0].

Example 3:

Input:

Output:

Explanation:

The constructed binary tree’s postorder traversal is [2, 5, 4, 3, 1].

Constraints:

  • 1 ≤ number of nodes ≤ $10^3$

  • 0 ≤ node->data ≤ $10^3$

  • Both inorder and preorder arrays contain unique values.

My Approach

Recursive Construction with Hash Map

  1. Identify the Root:

    • The first element in the preorder array is always the root of the tree.

  2. Index Lookup Using Hash Map:

    • Build a hash map that stores the index of each element in the inorder array for O(1) lookups.

  3. Recursive Tree Construction:

    • Using the current root from preorder, find its index in inorder.

    • Recursively construct the left subtree with the elements to the left of the root in inorder.

    • Recursively construct the right subtree with the elements to the right of the root in inorder.

  4. Output Generation:

    • Once the tree is constructed, its postorder traversal represents the expected output.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(N), as every node is processed exactly once.

  • Expected Auxiliary Space Complexity: O(N), mainly due to the recursion stack (in the worst-case scenario for a skewed tree) and the hash map used for index storage.

Code (C++)

🌲 Alternative Approaches

2️⃣ Iterative (Using Stack)

3️⃣ Recursive (Traditional)

Comparison of Approaches

Approach
Time Complexity
Space Complexity
Method
Pros
Cons

Recursive (Lambda)

🟢 O(N)

🟡 O(N)

Recursion (Lambda)

Clean, concise, minimal code footprint

May hit recursion limits on very deep trees

Iterative (Stack)

🟢 O(N)

🟡 O(N)

Stack-based

Avoids recursion depth issues; explicit control

Slightly more complex to implement

Recursive (Traditional)

🟢 O(N)

🟡 O(N)

Recursion

Straightforward and familiar recursive pattern

Recursion stack may overflow in worst-case scenarios

Best Choice?

  • For balanced trees, the Recursive (Lambda) approach is ideal.

  • For deep or skewed trees, the Iterative (Stack) approach is recommended.

  • For general usage with simplicity, the Recursive (Traditional) approach works well.

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated