Problem
You get two integer arrays, preorder and inorder. They are two traversals of the same binary tree. A traversal is a list of all the node values in one fixed visit order.
In a binary tree, each node has at most two children: a left child and a right child. The left subtree of a node is its left child and all the nodes below that child. The right subtree of a node is its right child and all the nodes below that child.
A preorder traversal visits the node first, then its left subtree, then its right subtree. An inorder traversal visits the left subtree first, then the node, then the right subtree. Both traversals use the same order inside each subtree.
Build the tree and return its root. All the values in the tree are different.
The examples show the output tree as a list in level order. This list gives the nodes row by row, from left to right. The value null marks a missing child.
Example 1
- Input:
- preorder = [8,5,2,6,11], inorder = [2,5,6,8,11]
- Output:
- [8,5,11,2,6]
- Explanation:
- The first value of
preorder,8, is the root. Ininorder, the values on the left of8make the left subtree.
Example 2
- Input:
- preorder = [4,7], inorder = [4,7]
- Output:
- [4,null,7]
- Explanation:
- In
inorder, no value comes before4. So the root4has no left child.
Example 3
- Input:
- preorder = [3,2,1], inorder = [1,2,3]
- Output:
- [3,2,null,1]
Constraints
- 1 <= preorder.length <= 3000
- inorder.length == preorder.length
- -3000 <= preorder[i], inorder[i] <= 3000
- All the values in preorder are different.
- Each value of inorder is also in preorder.
- preorder and inorder are traversals of the same binary tree.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Construct Binary Search Tree from Preorder Traversal
Coding
- Construct Binary Tree from Inorder and Postorder Traversal
Coding
- Construct Binary Tree from Preorder and Postorder Traversal
Coding
- Binary Tree Inorder Traversal
Coding · Binary Tree · Depth-First Search · Stack
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Microsoft
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Reported May 1, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source