Construct Binary Tree from Inorder and Postorder Traversal

Medium Trees & BST Tree Serialization / Deserialization Original on LeetCode

Construct Binary Tree from Inorder and Postorder Traversal is a medium trees & bst problem solved with the tree serialization / deserialization pattern. The best approach, optimal (hash map of inorder positions), runs in O(n) time and O(n) space. Below are 2 approaches in Java, from recursive with linear search up.

Problem

Given the inorder and postorder traversals of a binary tree with unique values, rebuild the tree and return its root.

Examples

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 <= n <= 3000; values are unique.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Recursive with linear searchO(n²)O(h)
Optimal (hash map of inorder positions)O(n)O(n)

2Optimal (hash map of inorder positions)

TimeO(n)
SpaceO(n)

Precompute value → inorder index. Walk postorder backwards, building right before left.

  1. pos map from inorder.
  2. build(lo, hi): root = post[p--]; mid = pos[root]; right = build(mid + 1, hi); left = build(lo, mid - 1).
Java
class Solution {
    private int p;
    private final Map<Integer, Integer> pos = new HashMap<>();

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for (int i = 0; i < inorder.length; i++) pos.put(inorder[i], i);
        p = postorder.length - 1;
        return build(postorder, 0, inorder.length - 1);
    }

    private TreeNode build(int[] post, int lo, int hi) {
        if (lo > hi) return null;
        TreeNode root = new TreeNode(post[p--]);
        int mid = pos.get(root.val);
        root.right = build(post, mid + 1, hi);
        root.left = build(post, lo, mid - 1);
        return root;
    }
}

Edge cases to test

  • Skewed trees

Hints

Hint 1

The root is the last postorder value. Reading postorder from the end gives root, right subtree, left subtree, so build the right side first.

FAQ

What is the best time complexity for Construct Binary Tree from Inorder and Postorder Traversal?

Optimal (hash map of inorder positions) runs in O(n) time and O(n) extra space.

Which pattern does Construct Binary Tree from Inorder and Postorder Traversal use?

It is a trees & bst problem that uses the tree serialization / deserialization pattern. Other problems with the same pattern: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Search Tree from Preorder Traversal, Serialize and Deserialize Binary Tree.

Is there a brute force solution for Construct Binary Tree from Inorder and Postorder Traversal?

Yes. Recursive with linear search takes O(n²) time and O(h) space. Take roots from the end of postorder, find each in inorder by scanning, and build the right subtree before the left.

Which edge cases should I test for Construct Binary Tree from Inorder and Postorder Traversal?

Skewed trees.