29 · Topological Sort

#310 Minimum Height Trees

Mediumsolve on LeetCode ↗

frequently reported atGoogleAmazonMicrosoft

the problem

In plain words

Given a tree of n nodes (n − 1 undirected edges), choosing any node as the root gives some height. Return all roots that give the minimum height (in any order).

in n = 4, edges = [[1,0],[1,2],[1,3]]

out [1]

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

out [3,4]

  • 1 ≤ n ≤ 2·10⁴
  • edges.length == n − 1
  • The input is a tree

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #543
      Diameter of Binary Tree
      Easyin Tree Recursion
    2. #994
      Rotting Oranges
      Mediumin Graphs
    3. #261
      Graph Valid Tree
      Mediumin Union-Find & MSTLeetCode Premium