Skip to content

Deepest Level Count

Easy#dfs#recursion~15 min

The mission

Problem

Given the root of a binary tree, return the number of levels on the longest path from the root down to any leaf. A tree with only a root counts as one level, and an empty tree counts as zero.

Examples

Input:  tree = 3 with children 9 and 20, where 20 has children 15 and 7
Output: 3
Why:    the path 3, 20, 15 covers three levels
Input:  tree = 1 with a right child 2, whose right child is 3
Output: 3
Why:    a completely lopsided tree still counts every level
Input:  tree = empty
Output: 0
Why:    edge case, there are no levels to count

Hints

0 / 3

Target time
O(n)
Target space
O(h)

Constraints

  • the tree may be empty

Stuck on the idea rather than the code? Tree Depth and Balance covers it.

Python 3Ctrl+Enter runs