310. Minimum Height Trees

Practice Minimum Height Trees for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

You are given a tree with n nodes labeled 0 to n - 1 as a list of undirected edges. Picking any node as the root gives a rooted tree whose height is the number of edges on the longest path from the root down to a leaf. Return every node that gives the smallest possible height, in any order.

Example

n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]  ->  [3,4]

Constraints