Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Construct Tree from Inorder + Postorder

Medium Acceptance 65.12% Points 30

You are given two integer arrays, inorder and postorder, representing the inorder and postorder traversals of a binary tree.

Your task is to reconstruct and return the original binary tree.

Since all node values are unique, the binary tree can be reconstructed uniquely from the given traversals.

Examples
Example 1

image Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output: [3,9,20,null,null,15,7]
Explanation: The last element of postorder (3) is the root; splitting inorder around it reconstructs the left and right subtrees recursively.

Example 2

image Input: inorder = [1], postorder = [1]
Output: [1]
Explanation: A single value reconstructs a single-node tree.

Example 3

image Input: inorder = [], postorder = []
Output: []
Explanation: Empty traversals produce an empty tree.

Constraints
    • 0 = Number of nodes = 3000
    • -10? = Node.data = 10?
    • inorder and postorder consist of unique values
    • Each value in postorder also appears in inorder
Hints
  • The traversals follow these orders:
    • Inorder: Left ? Root ? Right
    • Postorder: Left ? Right ? Root
Topics
Tree Traversal Binary Tree
Companies
Zoho Nutanix
Added 08 Aug 2026 65.12% acceptance
Ln 1, Col 1Sign in to save your code
4 2 5 1 6 3 7
4 5 2 6 7 3 1
1 2 3 4 5 6 7