Skip to content
CodeShare
All problems

Maximum Depth of Binary Tree

Measure how many levels deep a binary tree goes.

EasyTrees

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.

Trees are written level by level, with null for a missing child. [3, 9, 20, null, null, 15, 7] is a root 3 with children 9 and 20, where 20 has children 15 and 7.

Examples

Example 1
Input
root = [3, 9, 20, null, null, 15, 7]
Output
3
Example 2
Input
root = [1, null, 2]
Output
2
Example 3
Input
root = []
Output
0

Constraints

  • The tree has between 0 and 10^4 nodes.
  • -100 <= Node.val <= 100