Skip to main content
Back to problems
Leetcode
Medium
Trees
Arrays
Recursion
Hash Maps
Google
Meta
Construct Binary Tree From Inorder And Postorder Traversal

Reconstruct a binary tree from its inorder and postorder traversal arrays.

Acceptance 80%
Problem Statement

Problem

You are given two integer arrays inorder and postorder that represent the inorder and postorder traversal of the same binary tree with unique values.

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

Because the values are unique, the tree is guaranteed to be reconstructible from these traversals.

Input Format

  • inorder: an array of unique integers representing an inorder traversal.
  • postorder: an array of the same unique integers representing a postorder traversal.

Both arrays describe the same binary tree.

Output Format

Return the root of the reconstructed binary tree.

Constraints

  • The tree nodes contain unique values.
  • inorder.length == postorder.length
  • The traversal arrays describe the same binary tree.
  • Typical interview setting: 1n1051 \le n \le 10^5 (implementation should be efficient).
Examples
Sample cases returned by the problem API.

Example 1

Input

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Output

[3,9,20,null,null,15,7]

Explanation

The last postorder value is 3, so 3 is the root. In inorder, 9 is on the left of 3 and 15,20,7 are on the right. Repeating the process reconstructs the full tree.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.