Structure and Measure
What is this?
Sometimes you don't want to change a tree, you just want to understand its shape. Is it neatly filled in, level by level, like seats packed into a theatre row? How few steps to the nearest exit? Where do two people's family lines first meet? These problems are about reading those facts off a tree, and the trick is picking the right way to walk it โ sweeping across levels for layout questions, or going deep so each node can hear back from its children.
๐ก Fun fact: A "complete" binary tree โ one filled level by level with no gaps โ is the secret backbone of the heap, the data structure that powers priority queues. That tidy gap-free shape lets the whole tree be stored in a plain flat array with no pointers at all.
๐ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: Before you can transform a tree, you have to read it. This bucket is about extracting facts from a binary tree's shape: is it filled in level by level, how shallow is its nearest leaf, and where do two nodes' paths first join. Two tools cover all three โ a level-order BFS that walks the tree row by row, and a post-order DFS that lets each node decide based on what its two children reported back.
The pattern
A binary tree carries two kinds of information: its layout (how nodes are arranged across levels) and its relationships (how nodes sit relative to one another on root-to-leaf paths). BFS is the natural lens for layout questions because it visits nodes in the exact order a "complete" tree would fill them. DFS โ specifically post-order โ is the lens for relationship questions, because a node can only judge itself once both children have answered.
The skill is matching the question to the traversal. "Is every level packed, left to right?" is a BFS gap-detection problem. "How few steps to the closest leaf?" is shortest-path, so BFS again. "Where do these two nodes meet?" is about subtree membership, so DFS.
The problems
- Check Completeness of a Binary Tree โ BFS the tree and watch for the first missing child; once you've seen a gap, every node after it must also be a leaf-level gap, or the tree isn't complete.
- Minimum Depth of Binary Tree โ the shallowest leaf. BFS finds it the instant it dequeues the first leaf (no need to scan the whole tree); a DFS works too but must be careful that a missing child is not a leaf.
- Lowest Common Ancestor of a Binary Tree โ post-order DFS returns whether each subtree contains a target; the node where the two targets surface from different children is the split point and the answer.
Key takeaways
- Match the traversal to the question: BFS for level/layout and shortest-distance facts, post-order DFS for relationships between nodes.
- BFS can stop early โ minimum depth is found at the first leaf dequeued, not after a full sweep.
- A "gap" flag turns completeness into a single pass โ once a hole appears, no later node may have children.
- Why interviewers like it: these problems reveal whether you reach for the right traversal on instinct rather than forcing one tool onto every shape.
Order: Minimum Depth โ Check Completeness โ Lowest Common Ancestor.
Minimum Depth of Binary Tree
Core idea: The minimum depth is the node-count of the shortest root-to-leaf path โ and a node with one missing child is not a leaf, so you must descend into the child that exists, never treat the empty side as depth zero.
Problem, rephrased
You manage a fleet of warehouse robots. Each robot sits at the root of a decision tree: every node is a checkpoint, and the robot can branch left or right at each one. A leaf is a final loading dock โ a checkpoint with no further branches. You want the fewest checkpoints the robot must pass through (counting the start and the dock) to reach any dock at all, because batteries are limited and you only care about the closest exit.
Formally: given the root of a binary tree, return the number of nodes along the shortest path from the root down to the nearest leaf node. A leaf is a node with neither a left nor a right child. An empty tree has depth 0.
The trap: a checkpoint that branches only one way is a corridor, not a dock. You cannot "exit" there.
| Input (tree, level-order) | Output | Why |
|---|---|---|
[] |
0 |
No nodes at all |
[7] |
1 |
Root is itself a leaf |
[1,2,3] |
2 |
Both children are leaves; nearest is one hop away |
[2,null,3,null,4] |
3 |
Right-only chain โ every node is a corridor until 4 |
[3,9,20,null,null,15,7] |
2 |
9 is a true leaf at depth 2 |
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson