Skip to main content

Construct Binary Tree from Inorder and Postorder Traversal - Solution & Explanation

MediumArrayHash TableDivide and ConquerTree18 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.

 

Example 1:

Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output: [3,9,20,null,null,15,7]

Example 2:

Input: inorder = [-1], postorder = [-1]
Output: [-1]

 

Constraints:

  • 1 <= inorder.length <= 3000
  • postorder.length == inorder.length
  • -3000 <= inorder[i], postorder[i] <= 3000
  • inorder and postorder consist of unique values.
  • Each value of postorder also appears in inorder.
  • inorder is guaranteed to be the inorder traversal of the tree.
  • postorder is guaranteed to be the postorder traversal of the tree.

Approach Overview

Problem Overview: You are given two traversal arrays of the same binary tree: inorder and postorder. The goal is to reconstruct the original binary tree. The challenge is determining how to split the tree correctly using the ordering properties of both traversals.

Approach 1: Recursive Divide and Conquer with HashMap (O(n) time, O(n) space)

The key observation comes from traversal properties. In postorder, the last element is always the root of the current subtree. Once you identify the root, you locate its position inside the inorder array. Everything to the left of that index belongs to the left subtree, and everything to the right belongs to the right subtree. To avoid repeatedly scanning the inorder array, store each value's index in a HashMap for constant-time lookups.

The algorithm processes the postorder array from the end. Each recursive call creates a node, splits the inorder range, and recursively builds the right subtree first, then the left subtree. Building the right subtree first is necessary because postorder processes nodes as left → right → root, so when iterating backwards the order becomes root → right → left. Each node is created exactly once, producing O(n) time complexity with O(n) space for recursion and the hash map. This approach combines divide and conquer with efficient lookups using a hash table.

Approach 2: Iterative Construction using Stack (O(n) time, O(n) space)

An iterative strategy avoids recursion by simulating the construction process with a stack. Start from the last element of postorder, which is the root, and push it onto the stack. Traverse the postorder array backwards while tracking an index in the inorder array. The stack represents the path of nodes whose left child hasn't been attached yet.

If the top of the stack doesn't match the current inorder value, the next node becomes the right child of the stack's top node. If it matches, pop nodes from the stack until the values diverge; the next created node becomes the left child of the last popped node. This mirrors how inorder traversal signals that the right subtree has finished and the algorithm should move left. Each node is pushed and popped at most once, giving O(n) time complexity and O(n) auxiliary stack space. The approach is useful when you want to avoid deep recursion while working with binary tree construction problems.

Recommended for interviews: The recursive HashMap approach is the expected solution. It clearly demonstrates understanding of traversal properties and divide and conquer. Interviewers usually want to see the insight that the last element of postorder is the root and that a hash map reduces the lookup cost to O(1). The iterative stack version shows deeper mastery but is less commonly expected as the primary solution.

Approach 1: Recursive Approach using HashMap

This approach uses recursion and a HashMap to efficiently construct the binary tree. The key insight is that the last element in the postorder array is the root node. This node's index in the inorder array can be found using a HashMap, allowing constant time access during recursive calls.

This Python solution employs a recursive helper function to build the tree. The postorder index is decremented each time a new root is established, while the hashmap provides constant-time access to any given node's inorder index.

Code

Python

Java

Complexity

Time Complexity: O(n), where n is the number of nodes. Each node is visited once.
Space Complexity: O(n), where n is the number of nodes for storing the map and recursion stack.

Try this approach in the editor →

Approach 2: Iterative Approach using Stack

This approach builds the tree using an iterative method with a stack. It tracks nodes that need a child and assigns them accordingly. It takes advantage of the tree properties in the postorder and inorder arrays for efficient traversal and node placement.

This JavaScript solution uses a stack to iteratively build the tree. It maintains nodes that need children and assigns nodes based on whether they are different from the current inorder node. Nodes are popped from the stack when they match inorder traversal to handle left children appropriately.

Code

JavaScript

C#

Complexity

Time Complexity: O(n), where n is the number of nodes. Every element is seen once.
Space Complexity: O(n), where n is the number of nodes to account for the stack and tree storage.

Try this approach in the editor →

Approach 3: Hash Table + Recursion

The last node in the post-order traversal is the root node. We can find the position of the root node in the in-order traversal, and then recursively construct the left and right subtrees.

Specifically, we first use a hash table d to store the position of each node in the in-order traversal. Then we design a recursive function dfs(i, j, n), where i and j represent the starting positions of the in-order and post-order traversals, respectively, and n represents the number of nodes in the subtree. The function logic is as follows:

  • If n leq 0, it means the subtree is empty, return a null node.
  • Otherwise, take out the last node v of the post-order traversal, and then find the position k of v in the in-order traversal using the hash table d. Then the number of nodes in the left subtree is k - i, and the number of nodes in the right subtree is n - k + i - 1.
  • Recursively construct the left subtree dfs(i, j, k - i) and the right subtree dfs(k + 1, j + k - i, n - k + i - 1), connect them to the root node, and finally return the root node.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Recursive Approach using HashMap

Time Complexity: O(n), where n is the number of nodes. Each node is visited once.
Space Complexity: O(n), where n is the number of nodes for storing the map and recursion stack.

Iterative Approach using Stack

Time Complexity: O(n), where n is the number of nodes. Every element is seen once.
Space Complexity: O(n), where n is the number of nodes to account for the stack and tree storage.

Hash Table + Recursion—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive Divide and Conquer with HashMapO(n)O(n)Best general solution. Clean recursion and constant-time inorder lookups.
Iterative Construction using StackO(n)O(n)Useful when avoiding recursion or when stack-based tree construction is preferred.

Video Solution

L35. Construct the Binary Tree from Postorder and Inorder Traversal | C++ | Java • take U forward • 211,832 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Construct Binary Tree from Inorder and Postorder Traversal easy or hard?
The problem is classified as Medium difficulty on most coding platforms. The logic becomes straightforward once you recognize that postorder's last element is the root and inorder defines subtree boundaries. Efficient solutions require understanding recursion, tree traversal patterns, and hash map optimization.
Construct Binary Tree from Inorder and Postorder Traversal Python/Java solution
In Python or Java, the standard implementation uses recursion with a HashMap (or dictionary) that maps node values to their positions in the inorder array. The algorithm processes the postorder list from the end, constructs the root, splits the inorder range, and recursively builds right and left subtrees.
How to solve Construct Binary Tree from Inorder and Postorder Traversal in O(n)?
Process the postorder array from the end because the last element represents the root. Use a HashMap to quickly locate that root in the inorder array, which divides the subtree ranges. Recursively build the right subtree first and then the left subtree while shrinking the inorder boundaries. Each element is processed once, producing O(n) time complexity.
What is the best approach for Construct Binary Tree from Inorder and Postorder Traversal?
The most efficient approach uses recursion with a HashMap to store inorder indices. The last element of the postorder array is always the root, and the inorder index splits the tree into left and right subtrees. Using a hash map avoids repeated searches in the inorder array, resulting in O(n) time complexity and O(n) space complexity.
Is Construct Binary Tree from Inorder and Postorder Traversal asked at Google/Amazon/Meta?
Binary tree reconstruction problems appear frequently in interviews at companies like Amazon, Google, Meta, and Microsoft. Variants include building a tree from preorder and inorder traversals or verifying traversal sequences. The problem tests understanding of tree traversal properties and divide-and-conquer recursion.
What data structure is used in Construct Binary Tree from Inorder and Postorder Traversal?
The main data structures are arrays for the traversal inputs, a hash map for fast index lookup in the inorder array, and a binary tree structure for the result. Some implementations also use a stack for an iterative solution that simulates recursion.
What is the time complexity of Construct Binary Tree from Inorder and Postorder Traversal?
The optimal solution runs in O(n) time where n is the number of nodes in the tree. Each node is created exactly once, and the HashMap allows constant-time lookup of the root index in the inorder array. Space complexity is O(n) due to recursion depth and the auxiliary map.

Ready to solve this problem?

Practice Construct Binary Tree from Inorder and Postorder Traversal with our built-in code editor and test cases.

Practice on FleetCode