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

Lesson 10.4 of 14 in this module Unrolled Pro

Same Tree

Decide whether two binary trees have the same shape and values. From serialising both trees with markers for empty children and comparing the strings (O(n) extra memory, no early exit), to walking both trees together and stopping at the first difference. Then 'is one tree a subtree of another', from O(m · n) checks to hashing every subtree once; finding duplicate subtrees with a hash map; comparing models or configs by hashing their structure; and floating-point values that are equal only up to a tolerance.

about 10 min 4 Python examples 2 quick checks

Problem. Given the roots of two binary trees p and q, return whether they are the same: identical shape and equal values at every corresponding node.

text
p: 1        q: 1          →  True
  / \         / \
 2   3       2   3

p: 1        q: 1          →  False   (2 is a left child in p, a right child in q)
  /             \
 2               2

Comparing two trees in lockstep is a pattern you'll reuse: symmetry (lesson 10.3), subtree matching (Change A), merging trees, and diffing tree-shaped data.

ML context. "Are these two structures identical?" comes up constantly: has a model's module tree changed between checkpoints, are two experiment configs the same, can a cached compilation of a computation graph be reused for this graph? At scale you don't compare structures pairwise; you hash them so equal structures get equal fingerprints (Changes A to C).


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 "Same Tree"

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