◀ THE GRIND — GRAPHS

Minimum Height Trees

MEDIUM✓ CHIP-TIMEDLC #310 — FULL STATEMENT ↗

The drill: Given a tree of n nodes, pick every node that — if made the root — gives the shortest possible tree, measured by the longest path down from that root. There are at most two such nodes; return all of them, in any order.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A tree of n nodes arrives as a plain list of undirected edges, with no fixed root. The task is to figure out which node, if picked as the root, produces the shortest possible tree — measured by the longest path from that root down to any leaf.

Different roots can give wildly different heights for the same tree; rooting near either end of the tree's longest path always produces the tallest result, while rooting near the true middle produces the shortest.

At most two nodes can ever tie for that minimum height, and both would sit adjacent to each other at the tree's structural center. The answer is every node achieving that minimum, in any order.

EX 01
n = 4 · edges = [[1, 0], [1, 2], [1, 3]]
[1]
A STAR — THE HUB IS THE ONLY CENTER
EX 02
n = 6 · edges = [[3, 0], [3, 1], [3, 2], [3, 4], [4, 5]]
[3, 4]
TWO ADJACENT NODES TIE FOR CENTER
EX 03
n = 1 · edges = []
[0]
A SINGLE NODE IS ITS OWN CENTER
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

Rooting a tree at different nodes changes its height. The node that minimizes height sits as close as possible to every leaf — nowhere near the edges of the tree's longest path.

HINT 2 THE STRUCTURE

Rooting at either end of the tree's longest path gives the worst possible height. The best root, or two adjacent best roots, sit at the exact middle of that path — the tree's center.

HINT 3 ONE STEP FROM THE ANSWER

Repeatedly strip away all current leaves at once, layer by layer, the way an onion loses its skin. Whatever one or two nodes are left standing when the peeling stops are the tree's center.

COACH'S BOARD — THE PATTERN, STEP BY STEP
PEELING TO THE CENTERPATTERN · PEEL THE LEAVESn = 6 · edges: (3,0) (3,1) (3,2) (3,4) (4,5)
STEP 1

6 nodes. Repeatedly peel every current leaf — a degree-1 node — until 1 or 2 remain; those are the centers.

STEP 1 / 5 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/minimum-height-trees.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
        if n == 1:
            return [0]
        if n == 2:
            return [0, 1]

        graph = [set() for _ in range(n)]
        for a, b in edges:
            graph[a].add(b)
            graph[b].add(a)

        leaves = [i for i in range(n) if len(graph[i]) == 1]
        remaining = n
        while remaining > 2:
            remaining -= len(leaves)
            next_leaves = []
            for leaf in leaves:
                neighbor = graph[leaf].pop()
                graph[neighbor].discard(leaf)
                if len(graph[neighbor]) == 1:
                    next_leaves.append(neighbor)
            leaves = next_leaves

        return leaves
TIME O(V+E)SPACE O(V+E)PYTHON · RACE PACE · 25 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED