</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

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.

flowchart TD A["Ask a question about shape"] --> B["Layout question -> sweep level by level"] A --> C["Relationship question -> go deep first"] B --> D["Read the fact off the walk"] C --> D["Read the fact off the walk"]

๐Ÿ’ก 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.

Check Completeness of a Binary Tree

Core idea: A complete tree fills like water poured in row by row, left to right โ€” so once you hit the first empty slot, everything after it must be empty too.

Problem, rephrased

Imagine you run a stadium and you seat fans row by row, strictly left to right. Row 1 (one seat) must be full before anyone sits in row 2 (two seats), which must be full before row 3 (four seats), and so on. The only row allowed to be partially filled is the last one being seated โ€” and even there, seats must fill left-to-right with no gaps in between.

You're handed a seating chart shaped as a binary tree (root = the front seat, each seat has up to two seats behind-left and behind-right). Your job: report whether the fans were seated by the rules โ€” return True if the tree is complete, False otherwise.

A tree is complete when every level is fully filled except possibly the last, and the last level's nodes are pushed as far left as possible.

Input (tree, level-order) Output Why
[1,2,3,4,5,6] True Every level full except last; last fills left-to-right
[1,2,3,4,5,null,7] False Slot 6 (3's left child) is empty, but slot 7 (3's right child) is filled
[1,2,3,4,null,6] False Slot 5 (2's right child) is empty, then slot 6 reappears โ€” a gap in the middle
[1] True Single node
[] True Empty tree is vacuously complete

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

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.