Tensorum
LeetCode 106 · Construction / Serialization

Construct Binary Tree from Inorder and Postorder Traversal

LeetCode 106 · Medium · solution, intuition & step-by-step walkthrough

Root = postorder[end]; split by in-order index.

MediumArrayHash TableDivide and ConquerTreeBinary Tree
3 msruntime · beats 77%
21.2 MBmemory · beats 38%
1approach
6accepted runs · last 16 Mar 2026

This is how I solved Construct Binary Tree from Inorder and Postorder Traversal (LeetCode 106), a medium problem from the Construction / Serialization pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Same idea as pre-order + in-order, but post-order's last value is the root. Its position in the in-order list splits both lists into left and right parts.

Approach

  1. 1

    Index map for in-order values.

  2. 2

    Root from post_end.

  3. 3

    Left subtree uses the first nums_on_left of the post-order range.

  4. 4

    Right subtree uses the rest except the root.

How I solved it

Recursive split with index mapPythonO(n) time
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        """
        inorder - left,root,right
        postoder - left,right,root
        inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
        """
        in_map = {}
        m = len(inorder)
        n = len(postorder)
        for i,num in enumerate(inorder):
            in_map[num]=i
        def build(in_start,in_end,post_start,post_end):
            if in_start>in_end:
                return None
            if post_start>post_end:
                return None
            root = TreeNode(postorder[post_end])
            root_in_inmap_index = in_map[postorder[post_end]]
            nums_on_left= root_in_inmap_index-in_start
            root.left=build(in_start, root_in_inmap_index-1,post_start,post_start+nums_on_left-1)
            root.right=build(root_in_inmap_index+1, in_end,post_start+nums_on_left,post_end-1)
            return root
        return build(0,m-1,0,n-1)
        
Loading the step-through…

Construct Binary Tree from Inorder and Postorder Traversal — FAQ

What is the intuition behind Construct Binary Tree from Inorder and Postorder Traversal?

Same idea as pre-order + in-order, but post-order's last value is the root. Its position in the in-order list splits both lists into left and right parts.

How do you solve Construct Binary Tree from Inorder and Postorder Traversal (LeetCode 106)?

1. Index map for in-order values. 2. Root from post_end. 3. Left subtree uses the first nums_on_left of the post-order range. 4. Right subtree uses the rest except the root.

What is the time and space complexity of Construct Binary Tree from Inorder and Postorder Traversal?

Time: O(n). Space: O(n). (Accepted approach: Recursive split with index map.)

What are common mistakes in Construct Binary Tree from Inorder and Postorder Traversal?

Careful with the post-order ranges — the root sits at the end.