Minimum Height Trees
Problem
You are given a tree with n nodes labelled 0 to n - 1, described by a list of n - 1 undirected edges. Any node can be chosen as the root; rooting the tree at node r gives it a height equal to the number of edges on the longest downward path from r. Return the labels of every node that, when chosen as root, yields the minimum possible height. Return them in any order.
- There are at most 30,000 nodes.
edgeshas exactlyn - 1entries and forms a single connected tree.
Examples
n = 5, edges = [[2,0],[2,1],[2,3],[2,4]]→[2]. Rooting at the hub gives height 1; any leaf gives height 2.n = 6, edges = [[0,2],[0,3],[0,4],[0,1],[5,1]]→[0, 1]. Both have height 2; the tree's longest path has 4 nodes, so the two middle ones tie.n = 1, edges = []→[0]. The only node.
Hints
- Computing the height from every candidate root is
ntraversals. Which nodes are obviously bad roots? - If you delete every leaf at once, does the set of best roots change?
- Repeatedly strip all current leaves (nodes of degree 1) in rounds; the last one or two nodes remaining are the answer. This is Kahn's algorithm applied to an undirected tree.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.