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

Lesson 10.5 of 14 in this module Unrolled Pro

Diameter of Binary Tree

The longest path between any two nodes, measured in edges, which need not pass through the root. From computing the heights of both subtrees separately at every node (O(n²) on a chain), to one post-order pass that returns each subtree's height and updates a running best with left + right (O(n)). Then edge weights, trees with any number of children (the two tallest children), recovering the path itself, and the ML twin: the critical path through a tree of dependent tasks, which sets the minimum possible wall-clock time.

about 10 min 3 Python examples 2 quick checks

Problem. Return the length of the longest path between any two nodes, counted in edges. The path may or may not pass through the root.

text
      1
     / \          →   3      (4 → 2 → 1 → 3, or 5 → 2 → 1 → 3)
    2   3
   / \
  4   5

The new idea compared with lesson 10.2: the function returns one thing to its parent (the height) but computes another along the way (the best path seen so far). Many hard tree problems have this shape: lesson 10.13's maximum path sum is the same pattern with values instead of counts.

ML context. In a tree of dependent tasks (a preprocessing pipeline that fans out, a hierarchical evaluation suite, a tree-shaped computation where children must finish before parents), the longest weighted path is the critical path: the minimum wall-clock time even with unlimited parallel workers (Change D).


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 "Diameter of Binary 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