2Brute2Best
🔥0

Minimum Height Trees

Medium·LeetCode #310 Pro OnlyPattern Transfer
LeetCode
01

Problem Statement

You are given a tree with n nodes labelled 0 to n − 1, and its n − 1 undirected edges. Any node can be the root. The height of the tree rooted at a node is the number of edges on the longest path from that root down to a leaf.

Return the labels of every root that gives the smallest height. These roots are the minimum height trees (MHTs). You can return them in any order.

Input

n, the number of nodes, and edges, a list of n − 1 undirected pairs [a, b].

Output

A list with the labels of all roots that give the minimum height.

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

Rooted at 1, the height is 1: every other node is one step away. Rooted at any leaf, the height is 2.

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

Rooted at 3 or at 4, the height is 2. Every other root gives a height of 3 or more, for example root 0 reaches 5 through 3 and 4.

Constraints
  • • 1 ≤ n ≤ 2 × 10^4
  • • edges.length == n − 1
  • • 0 ≤ a, b < n
  • • a ≠ b
  • • All the pairs (a, b) are distinct.
  • • The input is a tree (connected, no cycles, no repeated edges).
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“tree with n nodes and n − 1 edges”
→ No cycles, and every node has a leaf path to the middle; peeling leaves keeps it connected.
“smallest height over all roots”
→ The answer is the centre of the tree, not one specific root.
“one or two answers”
→ The centre can be one node or two neighbours, so the stopping rule is 2 nodes left.
03

Approach Complexity Ladder

Brute Force — BFS from every nodeO(N²)O(N)

BFS from each node to find its height, then keep the smallest

Optimal — peel the leaves layer by layerO(N)O(N)

Remove all current leaves in rounds until at most 2 nodes are left; those are the answer

Brute Force Intuition

For each node r, run a BFS from r. The largest distance it finds is the height of the tree rooted at r. Keep every r with the smallest height. A tree has one or two such roots.

Recognition rule for this sub-pattern:

Queue all in-degree-0 nodes; removing one lowers its neighbours' in-degree.
Remember this · Topological Sort
Trigger
Prerequisites, dependencies, build order, "can all courses be finished?", ordering letters from sorted words.
Think
Is this a directed graph where some things must happen before others?
Key question
Which nodes currently have in-degree 0?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.