‹ DS&A interview · Socratic
DSA · Trees · #112

Maximum Depth of Binary Tree

Module 44 · difficulty 2/5·30:00starts on first keystroke

Given the `root` of a binary tree, return its **maximum depth**. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Each node is an object `{ val, left, right }`, where `left` and `right` are either child nodes or `null`. An empty tree is represented by `root === null`. Implement `maxDepth(root)`.

Examples
  • root = [3,9,20,null,null,15,7] 3The longest root-to-leaf path is 3 -> 20 -> 15 (or 3 -> 20 -> 7), 3 nodes deep.
  • root = [1,null,2] 2Path 1 -> 2 is 2 nodes deep.
  • root = [] 0An empty tree has depth 0.
Constraints
  • · The number of nodes is in the range [0, 10^4].
  • · -100 <= Node.val <= 100
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.