Construct Binary Tree from Preorder and Inorder
A medium Binary Trees problem included in Apna College, Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Binary Trees
- Sheets
- 3
- Core for
- 4 roles
- Platform
- LeetCode
The problem
Given two integer arrays preorder and inorder, construct and return the binary tree. preorder is the preorder traversal and inorder is the inorder traversal of the same tree.
Example 1
- Input
- preorder=[3,9,20,15,7], inorder=[9,3,15,20,7]
- Output
- [3,9,20,null,null,15,7]
- Why
- The first element 3 is the root. In inorder, 9 is left of 3 and 15,20,7 are right. Recursively build left and right subtrees.
Example 2
- Input
- preorder=[-1], inorder=[-1]
- Output
- [-1]
- Why
- A single node tree from a single-element traversal.
Example 3
- Input
- preorder=[1,2], inorder=[2,1]
- Output
- [1,null,2]
- Why
- Root is 1, 2 appears before 1 in inorder so it is the left child.
Constraints
- 1 <= preorder.length <= 3000
- inorder.length == preorder.length
- -3000 <= Node.val <= 3000
- All values in preorder and inorder are unique.
How to think about it
Updated 2026-09-09Preorder tells you who the root is: it is always the very next unused element. Inorder tells you where the boundaries lie: everything to the left of that root value forms its left subtree, and everything to the right forms its right subtree. Use a hash map of inorder indices to split without scanning.
Approaches, worst first
Recursive slicing with linear search
time O(n^2) · space O(n^2)
Take `preorder[0]` as root. Linear scan `inorder` to locate the root index, then slice both arrays into left and right sub-arrays and recurse. Array slicing and scanning takes quadratic time and extra allocations.
Hash map boundary recursionWrite this one
time O(n) · space O(n)
Pre-index `inorder` values to their indices in a hash map. Recurse using boundary pointers `(inStart, inEnd)` and a global preorder pointer. Root lookup is O(1), and calculating subtree size defines exact subarray boundaries instantly.
Where people lose marks · 3
- Calculating wrong index offsets when splitting preorder; the left subtree size is `rootIdx - inStart`, so preorder left spans `preStart + 1` to `preStart + size`.
- Omitting the hash map results in O(n^2) runtime, which causes timeouts on large inputs.
- Failing to enforce base case `inStart > inEnd` causes infinite recursion.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
What each operation costs
| Operation | Time |
|---|---|
| traverse all nodes using recursion or queue | O(n) |
| search for an arbitrary value in an unordered tree | O(n) |
| call stack memory on balanced tree | O(log n) |
| call stack memory on skewed tree | O(n) |
What usually goes wrong with Binary Trees
- Dereferencing left or right child pointers without checking if the current node is null, throwing null pointer errors on empty trees or leaf nodes.
- Defining a leaf node incorrectly by stopping when either child is null instead of checking that both left and right children are simultaneously null.
- Computing tree diameter by taking left height plus right height inside a recursive helper without updating a global maximum across every visited node.
Which roles need this problem
Binary Trees is a core topic for these 4 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including Full-Stack Developer, Android Developer, iOS Developer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Binary Trees problems
Problem set and role mapping as of .