# Construct a binary tree from preorder and inorder traversals

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/3417/construct-binary-tree-preorder-inorder-traversal
Source observed: 2026-11-05T00:29:27.002Z
Last checked: 2026-11-05T00:29:27.002+00:00
Indexed: 2026-11-07T00:46:01.591+00:00

---

# Construct a binary tree given its preorder and inorder traversals

**Difficulty:** Medium  
**Category:** Data Structures & Algorithms; Coding  
**Roles:** Software Engineer, Machine Learning Engineer  
**Companies associated with this question:** Amazon, Meta, Microsoft, Adobe, Google  
**Reported last asked:** 2 months ago  
**Interview reports:** 26  
**Saved:** 31

## Problem

Given two integer arrays, `preorder` and `inorder`, which represent the pre-order and in-order traversal of a binary tree, construct and return the binary tree's root node.

- **Pre-order traversal:** root → left subtree → right subtree
- **In-order traversal:** left subtree → root → right subtree

## Examples

### Example 1

```text
preorder = [3, 9, 20, 15, 7]
inorder = [9, 3, 15, 20, 7]
output: [3, 9, 20, null, null, 15, 7]
```

The constructed tree is:

```text
    3
   / \
  9  20
     / \
    15  7
```

### Example 2

```text
preorder = [1, 2]
inorder = [2, 1]
output: [1, 2]
```

The constructed tree is:

```text
  1
 /
2
```

## Constraints

- The number of nodes is in the range `[1, 3000]`.
- `preorder` and `inorder` contain unique integers.
- `inorder` contains the same elements as `preorder`, in a different order.

## Access and practice

We offer an upgrade for detailed company statistics: [Upgrade](https://www.aced.io/upgrade?src=questions). You can [sign up to run code](https://www.aced.io/signup?src=lesson). The available exercise interface includes an editor, output, test results, and console views.

## Interview experiences

Two shared interview experiences accompany this question:

- **Amazon · 2 months ago — Software Engineer · Intern**: “It was well, interviewer was helpful and was asking mostly standard questions from leetcode. I prepared from Strivers A2Z sheet so it was enough. I could have explained my thinking better.” [Read the Amazon experience](https://www.aced.io/experiences/amazon-software-engineer-intern-interview-d74e7f)
- **Meta · 9 months ago — Software Engineer · Senior / L5**: “The process was first a phone screen followed by a virtual loop with four rounds. The phone screen was a single tree traversal coding problem, and the onsite had two coding rounds, one system design round, and one behavioral round. The most interesting part was a new AI coding section that I hadn't seen before. It sounded new enough that even the format felt a little flexible, but the expectation was still that I do the actual coding myself and only use AI in a limited way. Overall it was pretty challenging, and if I did it again I would spend more time on behavioral prep because that was my weakest part.” [Read the Meta experience](https://www.aced.io/experiences/meta-facebook-senior-software-engineer-interview-dd0a91)

## Related questions

- [Print the cousins of a given node in a binary tree.](https://www.aced.io/questions/1155/print-cousins-binary-tree)
- [Construct a binary tree from an ancestor matrix.](https://www.aced.io/questions/1159/construct-binary-tree-from-ancestor-matrix)
- [Implement inorder traversal for an N-ary tree.](https://www.aced.io/questions/4697/n-ary-inorder-traversal)

## Related courses

### [Machine Learning Engineer Interview Prep](https://www.aced.io/courses/ml-engineer)

We cover mock interviews, frameworks, and advice from senior candidates, including ML system design, core concepts, coding, and behavioral interviews, for roles at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other companies.

![Machine Learning Engineer Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/6UfkHxUyLQVfDHSfLNQtWa/1be1539a65b4f6f15201f5b573c0a578/ML_Engineer_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

### [Software Engineering Interview Prep](https://www.aced.io/courses/software-engineering)

We cover data structures, algorithms, system design, people management, and behavioral interviews, with mock interviews, frameworks, and advice from senior candidates for roles at Google, Amazon, Microsoft, Meta, Apple, and other companies.

![Software Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/4VkuYewBHUFIVUS5oJDW5h/02e833439b1289f25f38f13f81cbdca3/adobe_stock_optimized.jpeg?w=800&h=800&fm=webp&q=75)

## Our broader interview-preparation resources

We provide interview courses and practice across Product Management, Forward Deployed Engineering, Engineering Management, Software Engineering, System Design, Data Science, Machine Learning, and Data Engineering, plus AI Engineering, Investment Banking, Generative AI, Security Engineer, TPM, Solutions Architect, UX / Product Design, and BizOps & Strategy. We also provide interview experiences, coding questions, practice, coaching, resume review, salary negotiation, job referrals, resources, and related services.

**Company identity:** Aced, formerly Exponent.

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3417%2Fconstruct-binary-tree-preorder-inorder-traversal&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3417%2Fconstruct-binary-tree-preorder-inorder-traversal). Opening a form does not execute a tool.