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

Lesson 10.2 of 14 in this module Unrolled Pro

Maximum Depth of Binary Tree

The number of nodes on the longest root-to-leaf path. From listing every root-to-leaf path and measuring each (O(n · h) time and memory), to one post-order recursion, 1 + max(left, right) (O(n) time, O(h) stack), to level-order counting with a queue that has no recursion limit. Then the minimum depth with an early exit, depth of a model's module tree with any number of children, trees too deep for recursion, and limiting a decision tree's depth.

about 10 min 5 Python examples 2 quick checks

Problem. Given the root of a binary tree, return its maximum depth: the number of nodes along the longest path from the root down to a leaf.

text
    3
   / \          →   3       (3 → 20 → 15)
  9   20
     /  \
    15   7

empty tree      →   0

It's the simplest "combine the children" problem, and the height it computes reappears everywhere: balance checks (lesson 10.6), diameters (lesson 10.5), the O(h) space cost of every recursive tree algorithm.

ML context. Depth is the most important hyperparameter of a decision tree: max_depth limits how many questions a prediction can ask, which controls overfitting and inference latency (a prediction costs O(depth)). The depth of a model's module tree, of a nested config or of a parse tree tells you how deep a recursive walk will go and whether it will hit Python's recursion limit (Change 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 "Maximum Depth 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