Minimum Height Trees

A tree is an undirected graph where any two vertices are connected by exactly one path. Given a tree of n nodes labeled from 0 to n - 1 and an array of n - 1 edges indicating undirected edges, you can select any node as the root. The tree height is the number of edges on the longest path between the root and a leaf. Return a list of root labels resulting in minimum height trees (MHTs).

Constraints

  • 1 <= n <= 2 * 10^4
  • edges.length == n - 1
  • 0 <= ai, bi < n
  • ai != bi
  • All pairs (ai, bi) are distinct.
  • The input is guaranteed to be a tree with no repeated edges.
Saving… · 2 lines
class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
Console

Click Run to execute sample tests.