ExamAdda Logo

Construct Binary Tree from Inorder and Postorder Traversal

Easy

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.

You may assume that all values in the tree are unique.

Example 1

Input

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

Output

[3,9,20,15,7]

Explanation

The last element of postorder is 3, so 3 is the root. In inorder, 3 separates the left subtree [9] from the right subtree [15,20,7].

Example 2

Input

inorder = [-1]
postorder = [-1]

Output

[-1]

Explanation

There is only one node, so it becomes the root of the tree.

Constraints

1 <= inorder.length <= 3000
postorder.length == inorder.length
-3000 <= inorder[i]
postorder[i] <= 3000
All values are unique.
inorder and postorder represent the same binary tree.

Hints:

Hint 1

The last element of postorder is always the root of the current subtree.

Hint 2

Find the root in inorder to divide the remaining elements into the left and right subtrees, then recursively construct both parts.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 18, 2026

Last updated

Aug 19, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output