algorythms
Tree DFS
LC #104Easy

Maximum Depth of Binary Tree

Tree DFS
AmazonGoogleLinkedInMeta

Problem

Find the maximum depth (number of nodes along the longest path from root to a leaf).

treedfsrecursion

Constraints

  • 0 ≤ n ≤ 10⁴
  • -100 ≤ Node.val ≤ 100

Example

Inputroot = [3, 9, 20, null, null, 15, 7]
Output3
Why

Longest path: 3→20→15 or 3→20→7, both length 3

Hints — reveal one at a time