Algo-Sim › Advanced Graphs
Minimum Height Trees
Medium · Advanced Graphs · step through it in Java, Python and C++ on your own input
Peel the leaves off the tree layer by layer, like a topological sort from the outside in; the last one or two nodes are the roots of minimum height.
Problem
A tree has n nodes labelled 0..n − 1 and n − 1 undirected edges. Rooting it at a node gives some height. Return every node that gives the minimum height, in any order.
Constraints
- 1 ≤ n ≤ 2·10⁴
- edges.length = n − 1
- The edges form a tree
Edge cases to test
- n = 1:
[0] - n = 2: both nodes
- A star: the centre
Follow-up questions
- Why one or two answers?
- The roots of minimum height are the centres of the tree’s longest path, and a path has one or two middle nodes.
- What is another method?
- BFS from any node to the farthest one, BFS again to get the diameter, and take its middle node(s): O(n).
Solution
Java
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if (n == 1) return List.of(0);
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
int[] degree = new int[n];
for (int[] e : edges) {
adj.get(e[0]).add(e[1]);
adj.get(e[1]).add(e[0]);
degree[e[0]]++;
degree[e[1]]++;
}
List<Integer> leaves = new ArrayList<>();
for (int v = 0; v < n; v++) if (degree[v] == 1) leaves.add(v);
int remaining = n;
while (remaining > 2) {
remaining -= leaves.size();
List<Integer> next = new ArrayList<>();
for (int leaf : leaves) {
for (int nb : adj.get(leaf)) {
if (--degree[nb] == 1) next.add(nb);
}
}
leaves = next;
}
return leaves;
}Python
def find_min_height_trees(n, edges):
if n == 1:
return [0]
adj = [[] for _ in range(n)]
degree = [0] * n
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
degree[a] += 1
degree[b] += 1
leaves = [v for v in range(n) if degree[v] == 1]
remaining = n
while remaining > 2:
remaining -= len(leaves)
nxt = []
for leaf in leaves:
for nb in adj[leaf]:
degree[nb] -= 1
if degree[nb] == 1:
nxt.append(nb)
leaves = nxt
return leavesC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>> &edges) {
if (n == 1)
return {0};
vector<vector<int>> adj(n);
vector<int> degree(n);
for (vector<int> &e : edges) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
degree[e[0]]++;
degree[e[1]]++;
}
vector<int> leaves;
for (int v = 0; v < n; v++)
if (degree[v] == 1)
leaves.push_back(v);
int remaining = n;
while (remaining > 2) {
remaining -= int(leaves.size());
vector<int> next;
for (int leaf : leaves) {
for (int nb : adj[leaf]) {
if (--degree[nb] == 1)
next.push_back(nb);
}
}
leaves = next;
}
return leaves;
}
};
More Advanced Graphs problems
- Network Delay Time (Medium)
- Evaluate Division (Medium)
- Course Schedule II (Medium)
- Alien Dictionary (Hard)
- Redundant Connection (Medium)
- Number of Connected Components (Union-Find) (Medium)
- Accounts Merge (Medium)
- Minimum Spanning Tree (Kruskal) (Medium)
- Minimum Spanning Tree (Prim) (Medium)
- Min Cost to Connect All Points (Medium)
- Cheapest Flights Within K Stops (Medium)
- Bellman-Ford (negative weights) (Medium)
- Find the City (Floyd-Warshall) (Medium)
- Critical Connections in a Network (Hard)
- Articulation Points (Cut Vertices) (Hard)
- Strongly Connected Components (Hard)
- Reconstruct Itinerary (Hard)