Code RoomTree centroids
HardPrep Room Coding #1474

Tree centroids

CodingAlgorithms & data structuresSenior–Staff~35 min

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.

0:00 of about 35 min
InputExpectedGot
[4,[[1,0],[1,2],[1,3]]][1]not run yetsample