Minimum Height Trees
A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. Given a tree of `n` nodes labeled from `0` to `n - 1`, and an array of `n - 1` edges, return a list of all MHT root labels. You can return the answer in any order. **Examples:** ```typescript findMinHeightTrees(4, [[1,0],[1,2],[1,3]]) // [1] findMinHeightTrees(6, [[3,0],[3,1],[3,2],[3,4],[5,4]]) // [3, 4] ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(V)
- Space complexity: O(V)
Trimming leaves layer by layer reduces the tree to at most 2 centroids in linear time.
Follow-Up Questions Interviewers Ask About Minimum Height Trees
- Why can a tree have at most two centroids?
Any tree diameter path has either one midpoint (if diameter length is even) or two midpoints (if odd).
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.