Maximum Depth of Binary Tree
easyTests passed 2026-11-11v1.0.01. Core Invariant / The Trick
Post-order recurrence: an empty tree has depth `0`; otherwise `1 + max(maxDepth(left), maxDepth(right))`.
2. Complexity Bounds & Proof
Every node is visited once in post-order DFS with O(H) call stack depth.
3. Minimal Idiomatic Code
def maxDepth(root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + max(maxDepth(root.left), maxDepth(root.right))4. Failure Modes & Interview Follow-ups
Pitfalls:- Off-by-one confusion between depth by nodes (root = 1) vs height by edges (root = 0).
Follow-ups:- Minimum Depth of Binary Tree prefers BFS so the first leaf encountered terminates immediately without scanning deeper subtrees.
Personal Notes & Scratchpad (Local-First Yjs CRDT)
Saved instantaneously in IndexedDB. Streams live across peers via Hocuspocus when signed in.
Related Invariants
Browser Scratchpad
Console Output
Click Run or press Cmd/Ctrl+Enter to execute locally in a Web Worker.