</> 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.

Lowest Common Ancestor of a Binary Tree

Core idea: The LCA is the split point โ€” the first node from which your two targets descend into opposite subtrees (or which is itself one of the targets).

Problem, rephrased

Imagine the reporting hierarchy of a company drawn as a tree. The CEO sits at the root; every employee has exactly one manager (their parent) and may have direct reports (children). HR hands you two employees, p and q, and asks: "Who is the most junior manager who has authority over both of them?"

That manager is the lowest (deepest) common ancestor. It is the node closest to p and q such that both employees sit somewhere underneath it. A node counts as an ancestor of itself, so if p is q's manager (directly or many levels up), the answer is simply p.

This is a general binary tree โ€” there is no ordering rule like a BST. You cannot look at a value and decide "go left" or "go right." You have to actually search.

Inputs / outputs

Input Meaning Output
root root of a binary tree the TreeNode that is the LCA of p and q
p, q two distinct nodes, guaranteed to exist in the tree (returned as a node reference, not a value)

You are given node references, not values โ€” so identity comparison (node == p) is what matters, and duplicate values would not even be a concern.

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.