Construct Binary Tree from Preorder and Inorder Traversal
Given two integer arrays:
- preorder — traversal in Root → Left → Right order
- inorder — traversal in Left → Root → Right order
Construct the binary tree and return its root.
The values are unique.
Key Idea
The first element of preorder is always the root.
Find that root in inorder:
Left Subtree | Root | Right Subtree
Example 1
Input
preorder = [3,9,20,15,7] inorder = [9,3,15,20,7]
Output
[3,9,20,null,null,15,7]
Explanation
The first element of preorder is 3, so 3 is the root.
In inorder:
[9, 3, 15, 20, 7]
↑
Root
Everything before 3 belongs to the left subtree:[9]
Everything after 3 belongs to the right subtree:[15,20,7]
So we get:
3
/ \
9 20
/ \
15 7
Therefore, the constructed tree is:
[3,9,20,null,null,15,7]
Example 2
Input
Preorder: [-1] Inorder: [-1]
Output
[-1]
Explanation
There is only one element.
Therefore, that element is both the root and the entire tree: -1
So the constructed tree is: [-1]
Constraints
Hints:
Hint 1
The first element of preorder is always the root.
Hint 2
Find the root in the inorder array.
Left Subtree | Root | Right Subtree
Author & Technical Reviewer
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 12, 2026
Last updated
Aug 13, 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.
Expected Output