Skip to content
BytePatterns

Shallowest Leaf Depth

EasyTrees & BST#bfs#level-order~15m

Problem

Given the root of a binary tree, return the number of nodes on the shortest path from the root down to a leaf, where a leaf is a node with no children. An empty tree has depth 0. A node with only one child is not a leaf, so a path through it has to continue into that child.

Examples

Input:  tree = 6, left 2 (whose left is 1, whose left is 0), right 9
Output: 2
Why:    9 is a leaf one step below the root
Input:  tree = 1, right 2, whose right is 3
Output: 3
Why:    1 and 2 each have a child, so only 3 is a leaf
Input:  tree = empty
Output: 0
Why:    edge case, there are no nodes

Hints

0 / 3

Stuck on the idea rather than the code? Tree Basics covers it.