PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/TikTok

Reconstruct a binary tree from traversals

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of binary tree traversal properties, data structure manipulation, and algorithmic complexity involved in reconstructing a tree from preorder and inorder sequences.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Reconstruct a binary tree from traversals

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Coding: Build a Binary Tree from Two Traversals You are given two integer arrays representing traversals of the **same** binary tree: - `preorder`: node order is **root → left → right** - `inorder`: node order is **left → root → right** All values are **unique**. ### Task Reconstruct the binary tree and return its root node. ### Input - `preorder`: length `n` (1 ≤ n ≤ 10^5) - `inorder`: length `n` - Values are unique integers. ### Output - Return the root of the reconstructed binary tree (using a standard `TreeNode { val, left, right }` structure). ### Example - `preorder = [3, 9, 20, 15, 7]` - `inorder = [9, 3, 15, 20, 7]` The reconstructed tree is: - root = 3 - left child = 9 - right subtree rooted at 20 with children 15 and 7 ### Notes - Assume the input is valid (both traversals come from one binary tree). - Your solution should be efficient for large `n`.

Quick Answer: This question evaluates understanding of binary tree traversal properties, data structure manipulation, and algorithmic complexity involved in reconstructing a tree from preorder and inorder sequences.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Jan 11, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

Coding: Build a Binary Tree from Two Traversals

You are given two integer arrays representing traversals of the same binary tree:

  • preorder : node order is root → left → right
  • inorder : node order is left → root → right

All values are unique.

Task

Reconstruct the binary tree and return its root node.

Input

  • preorder : length n (1 ≤ n ≤ 10^5)
  • inorder : length n
  • Values are unique integers.

Output

  • Return the root of the reconstructed binary tree (using a standard TreeNode { val, left, right } structure).

Example

  • preorder = [3, 9, 20, 15, 7]
  • inorder = [9, 3, 15, 20, 7]

The reconstructed tree is:

  • root = 3
  • left child = 9
  • right subtree rooted at 20 with children 15 and 7

Notes

  • Assume the input is valid (both traversals come from one binary tree).
  • Your solution should be efficient for large n .

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Software Engineer•TikTok Software Engineer•TikTok Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.