VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 10: Trees and BSTs

Lesson 10.11 of 14 in this module Unrolled Pro

Construct Binary Tree from Preorder and Inorder

Rebuild a binary tree from its pre-order and in-order sequences (unique values). From recursion that searches for the root in the in-order list and slices both lists (O(n²) time and copies), to a hash map of in-order positions and index bounds instead of slices (O(n)). Then post-order plus in-order, a binary search tree from pre-order alone using value bounds, why pre-order plus post-order is ambiguous, and rebuilding a tree from a single traversal with explicit markers for empty children.

about 10 min 3 Python examples 2 quick checks

Problem. Given the pre-order and in-order traversals of a binary tree with unique values, rebuild the tree and return its root.

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

      3
     / \
    9   20
       /  \
      15   7

Two facts make it work. Pre-order lists the root first. In-order lists everything in the root's left subtree before it and the right subtree after it. So the first pre-order value is the root, its position in the in-order list splits the in-order list into left and right parts, and their sizes split the rest of the pre-order list the same way.

ML context. Rebuilding structure from traversals is how trees travel through flat formats: a parse tree stored as token sequences, a model's module tree saved as lists, a decision tree exported as arrays. Knowing which traversals determine a tree uniquely (and which don't, Change C) is what makes a serialisation format correct; lesson 10.13 designs one.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Construct Binary Tree from Preorder and Inorder"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee