Code Room
CodingHardcod-g911
Subject Topological sortLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

An undirected tree has n nodes labeled 0..n-1 and exactly n-1 edges. Rooting the tree at a node gives it a height (the longest path from the root to any leaf). Return all root labels that minimize the tree's height (the centroids), in ascending order. For n==1 return [0]; for n==2 return both nodes. There are at most two such roots.

Implement
min_height_trees(n: int, edges: list[list[int]]) → list[int]
Examples
in[4,[[1,0],[1,2],[1,3]]]out[1]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.