Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Minimum Height Trees

Hard

Topics
GraphBFSTopological Sort

Given a tree of n nodes labeled from 0 to n-1 with n-1 edges, return the number of nodes that could be the root of a Minimum Height Tree (MHT). A MHT is one where the longest path from root to any leaf is minimized.

Example 1

Input:  n = 4, edges = [[1,0],[1,2],[1,3]]
Output: 1

Example 2

Input:  n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
Output: 2

Constraints

  • 1 <= n <= 2*10^4
  • edges.length == n - 1
  • The graph is a valid tree.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access