We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
An easy trees problem, graded against 6 test cases (3 of them hidden).
Recursive traversal of binary trees and BSTs - depth, validation, and path problems.
Reach for it when you see: A TreeNode input, or anything about depth, ancestry, or in-order ordering.
More Treesproblems →The instinct is to copy `maxDepth` and swap `max` for `min`. That is wrong, and the failing case is a node with exactly one child: the missing side returns 0, `min` picks it, and the node reports depth 1 as though it were a leaf. A right-leaning chain would report 1 instead of its true depth.
The fix is to respect the definition - only a node with no children terminates a path. When exactly one child exists, you must descend into that child and ignore the empty side.
BFS avoids the special case more elegantly and is faster in the common case. Traversing level by level, the first node encountered with no children is by construction at the minimum depth, so you can return immediately - no need to explore a deep subtree that cannot contain a shallower leaf. On a tree with a shallow leaf near the root and a huge subtree elsewhere, that is the difference between reading a handful of nodes and reading all of them.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.