Browse curriculum

Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal: rebuild a binary tree from its preorder and inorder traversals: preorder names each root, and the root's inorder position splits its left and right subtrees.

Problem Understanding

Construct Binary Tree from Preorder and Inorder Traversal: given two integer arrays preorder and inorder of the same tree, with unique values, rebuild and return the tree.

Example: preorder = [3, 9, 20, 15, 7], inorder = [9, 3, 15, 20, 7] gives the tree [3, 9, 20, null, null, 15, 7].

Attempt 1: Slice and Search

Take preorder[0] as the root, find it in inorder with a linear scan, slice both arrays into left and right parts and recurse. It is correct, but every call scans and copies: O(n²) time on a skewed tree, and O(n²) memory in the slices.

The Intuition: Preorder Names Roots, Inorder Splits Them

Two facts about the traversals:

  1. Preorder is root, left subtree, right subtree. Reading it left to right gives the roots in exactly the order a depth-first build needs them.
  2. Inorder is left subtree, root, right subtree. Once you know a root, its position mid in inorder splits the range: [lo, mid − 1] is its left subtree and [mid + 1, hi] its right.

So keep one index next into preorder, and write build(lo, hi) over an inorder range: an empty range is null; otherwise read the root, look up mid in a value → index map, build the left range, then the right. Building left before right matters, because preorder lists the whole left subtree before the right one.

Construct Binary Tree from Preorder and Inorder Traversal Visualization

PREORDER GIVES ROOTS · INORDER SPLITS THEM

MAP INORDER INDEXES

preorder

next

3

0

9

1

20

2

15

3

7

4

inorder

9

0

3

1

15

2

20

3

7

4

Range

0..4

Built

0/5

call stack

No calls in progress

 

Map each inorder value to its index, then build the whole range, inorder[0..4]

Next

preorder[0] = 3 is the root of [9, 3, 15, 20, 7]. It sits at inorder 1: 1 value left of it, 3 right

Free preview: the first 9 of 28 steps

Tree

Enter any tree with unique values; its preorder and inorder are derived for you. The preorder pointer moves one root at a time, the current call's inorder range is filled with the root's position marked, and the tree below grows one node per root read.

Construct Binary Tree from Preorder and Inorder Traversal Dry Run

Input: preorder = [3, 9, 20, 15, 7], inorder = [9, 3, 15, 20, 7]

next Root Inorder range mid Left range Right range
0 3 [9, 3, 15, 20, 7] 1 [9] [15, 20, 7]
1 9 [9] 0 empty empty
2 20 [15, 20, 7] 3 [15] [7]
3 15 [15] 2 empty empty
4 7 [7] 4 empty empty

Every preorder value is used once; the result is [3, 9, 20, null, null, 15, 7].

Construct Binary Tree from Preorder and Inorder Traversal LeetCode Solution

A preorder index, an inorder index map and recursion over inorder ranges.

Construct Binary Tree from Preorder and Inorder Traversal Code
1var buildTree = function(preorder, inorder) {
2 const index = new Map();
3 inorder.forEach((val, i) => index.set(val, i));
4 let next = 0;
5
6 const build = (lo, hi) => {
7 if (lo > hi) return null;
8 const val = preorder[next++];
9 const root = new TreeNode(val);
10 const mid = index.get(val);
11 root.left = build(lo, mid - 1);
12 root.right = build(mid + 1, hi);
13 return root;
14 };
15
16 return build(0, inorder.length - 1);
17};

Construct Binary Tree from Preorder and Inorder Traversal in Python, Java, C++, TypeScript and C

Construct Binary Tree from Preorder and Inorder Traversal in Python
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        index = {val: i for i, val in enumerate(inorder)}
        self.next = 0

        def build(lo: int, hi: int) -> Optional[TreeNode]:
            if lo > hi:
                return None
            val = preorder[self.next]
            self.next += 1
            root = TreeNode(val)
            mid = index[val]
            root.left = build(lo, mid - 1)
            root.right = build(mid + 1, hi)
            return root

        return build(0, len(inorder) - 1)
Construct Binary Tree from Preorder and Inorder Traversal in Java
class Solution {
    private Map<Integer, Integer> index = new HashMap<>();
    private int next = 0;

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        for (int i = 0; i < inorder.length; i++) index.put(inorder[i], i);
        return build(preorder, 0, inorder.length - 1);
    }

    private TreeNode build(int[] preorder, int lo, int hi) {
        if (lo > hi) {
            return null;
        }
        int val = preorder[next++];
        TreeNode root = new TreeNode(val);
        int mid = index.get(val);
        root.left = build(preorder, lo, mid - 1);
        root.right = build(preorder, mid + 1, hi);
        return root;
    }
}
Construct Binary Tree from Preorder and Inorder Traversal in C++
class Solution {
    unordered_map<int, int> index;
    int next = 0;

    TreeNode* build(vector<int>& preorder, int lo, int hi) {
        if (lo > hi) {
            return nullptr;
        }
        int val = preorder[next++];
        TreeNode* root = new TreeNode(val);
        int mid = index[val];
        root->left = build(preorder, lo, mid - 1);
        root->right = build(preorder, mid + 1, hi);
        return root;
    }

public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        for (int i = 0; i < (int)inorder.size(); i++) index[inorder[i]] = i;
        return build(preorder, 0, inorder.size() - 1);
    }
};
Construct Binary Tree from Preorder and Inorder Traversal in TypeScript
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
    const index = new Map<number, number>();
    inorder.forEach((val, i) => index.set(val, i));
    let next = 0;

    const build = (lo: number, hi: number): TreeNode | null => {
        if (lo > hi) return null;
        const val = preorder[next++];
        const root = new TreeNode(val);
        const mid = index.get(val)!;
        root.left = build(lo, mid - 1);
        root.right = build(mid + 1, hi);
        return root;
    };

    return build(0, inorder.length - 1);
}
Construct Binary Tree from Preorder and Inorder Traversal in C
static int idx[6001];
static int next;

static struct TreeNode* build(int* preorder, int lo, int hi) {
    if (lo > hi) {
        return NULL;
    }
    int val = preorder[next++];
    struct TreeNode* root = malloc(sizeof(struct TreeNode));
    root->val = val;
    int mid = idx[val + 3000];
    root->left = build(preorder, lo, mid - 1);
    root->right = build(preorder, mid + 1, hi);
    return root;
}

struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) {
    for (int i = 0; i < inorderSize; i++) idx[inorder[i] + 3000] = i;
    next = 0;
    return build(preorder, 0, inorderSize - 1);
}

Edge Cases & Common Mistakes

  • One node: the root is built and both ranges are empty.
  • A skewed tree ([3, 2, null, 1]): every split puts all remaining values on one side, the map keeps it O(n) instead of O(n²).
  • Building right before left: the shared preorder index would hand the right subtree the left subtree's roots.
  • Duplicate values: the inorder position is ambiguous; the problem guarantees unique values.
  • Slicing arrays: correct, but copies cost O(n) per level, pass index ranges instead.
  • The C array offset: LeetCode limits values to −3000..3000, so val + 3000 indexes a fixed array instead of a hash map.

Summary

Read roots from preorder one at a time, and split each inorder range at the root's index: left subtree first, then right. With a value → index map, O(n) time and O(n) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Map inorder

    Store each value's inorder index in a hash map, and start a preorder index at 0.

  2. Read the root

    For the inorder range [lo, hi], take preorder[next] as the root and advance next. An empty range returns null.

  3. Split and recurse

    With mid as the root's inorder index, build the left subtree from [lo, mid − 1], then the right subtree from [mid + 1, hi].

Frequently asked questions

How do preorder and inorder traversals determine a binary tree?

Preorder visits a root before its subtrees, so its first value is the root. Inorder visits the left subtree, then the root, then the right subtree, so everything before the root's inorder position is its left subtree and everything after is its right. Repeating this on each part rebuilds the tree, provided the values are unique.


Why use a hash map of inorder indexes?

Each call needs the root's position in inorder. Scanning for it costs O(n) per call and O(n²) overall on a skewed tree. Mapping every value to its index first makes each lookup O(1), so the whole build is O(n).


Why must the left subtree be built before the right?

The recursion reads preorder with a single moving index. Preorder lists a root, then its whole left subtree, then its right subtree, so the left subtree must consume its values first or the right subtree would take the wrong roots.

New to the technique behind this problem? Watch it run step by step:

Depth First Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description