105. Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal (LeetCode 105) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Preorder Traversal subpattern.

How to solve Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal is a Preorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master preorder traversal for tree construction, inversion, and path problems. Visit root before children.

Work towards the solution in these steps:

  1. Recall that preorder traversal visits nodes in the order: root, left subtree, then right subtree; consider how this order can help identify the root at each step.
  2. Think about how the inorder traversal, which visits nodes in the order: left subtree, root, then right subtree, can help determine the boundary between left and right subtrees.
  3. Try to use recursion by selecting the root from the preorder array and then splitting the inorder array into left and right parts to build subtrees.

The reference solution runs in O(N) time and O(N) space.

The Construct Binary Tree from Preorder and Inorder Traversal solution and editorial walks through the full approach with reference code.

Problem statement

Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

Example 1:

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

Example 2:

Input: preorder = [-1], inorder = [-1] Output: [-1]

Constraints:

- 1 <= preorder.length <= 3000

- inorder.length == preorder.length

- -3000 <= preorder[i], inorder[i] <= 3000

- preorder and inorder consist of unique values.

- Each value of inorder also appears in preorder.

- preorder is guaranteed to be the preorder traversal of the tree.

- inorder is guaranteed to be the inorder traversal of the tree.

Topics and companies

Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree.

Reported in interviews at Amazon, Microsoft, Facebook, ByteDance, Citadel, Bloomberg, Oracle.

How to practise Construct Binary Tree from Preorder and Inorder Traversal on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(N) space.

Open Construct Binary Tree from Preorder and Inorder Traversal in the code editor, or read the Construct Binary Tree from Preorder and Inorder Traversal editorial for a worked solution with its approach and complexity analysis.

Where Construct Binary Tree from Preorder and Inorder Traversal sits in the DSA pattern sheet

Problems related to Construct Binary Tree from Preorder and Inorder Traversal

Other problems that use the same Recursive Preorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Construct Binary Tree from Preorder and Inorder Traversal FAQ

What is the LeetCode number of Construct Binary Tree from Preorder and Inorder Traversal?

Construct Binary Tree from Preorder and Inorder Traversal is LeetCode problem 105, rated Medium.

What pattern is used to solve Construct Binary Tree from Preorder and Inorder Traversal?

Construct Binary Tree from Preorder and Inorder Traversal is a Preorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Preorder Traversal subpattern.

What is the time complexity of Construct Binary Tree from Preorder and Inorder Traversal?

The reference solution runs in O(N) time and O(N) space.

Which companies ask Construct Binary Tree from Preorder and Inorder Traversal?

Construct Binary Tree from Preorder and Inorder Traversal has been reported in interviews at Amazon, Microsoft, Facebook, ByteDance, Citadel, Bloomberg, Oracle.

Preparing this workspace.