Leetcode 310. Minimum Height Trees
Given a tree with n
nodes, labeled from 0
to n-1
, and an array of n-1
edges where edges[i] = [a_i, b_i]
indicates an undirected edge between nodes a_i
and b_i
, you need to find the roots of the Minimum Height Trees (MHTs). A tree’s height is determined as the number of edges in the longest path from the root to any leaf. Your task is to return a list of possible root nodes for the MHTs.
Example:
Input: n = 4
, edges = [[1, 0], [1, 2], [1, 3]]
Output: [1]
Constraints:
1 <= n <= 2 * 10^4
edges.length == n - 1
0 <= a_i, b_i < n
(a_i, b_i)
are distinct.1 <= n
, so there’s at least one node.import java.util.*;
public class MinimumHeightTrees {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if (n == 1) return Collections.singletonList(0);
// Build the graph using adjacency list
List<Set<Integer>> graph = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
graph.add(new HashSet<>());
}
for (int[] edge : edges) {
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
// Initialize the first layer of leaves
List<Integer> leaves = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (graph.get(i).size() == 1) {
leaves.add(i);
}
}
// Trim the leaves until we reach the core of the graph
int remainingNodes = n;
while (remainingNodes > 2) {
remainingNodes -= leaves.size();
List<Integer> newLeaves = new ArrayList<>();
for (int leaf : leaves) {
int neighbor = graph.get(leaf).iterator().next();
graph.get(neighbor).remove(leaf);
if (graph.get(neighbor).size() == 1) {
newLeaves.add(neighbor);
}
}
leaves = newLeaves;
}
return leaves;
}
public static void main(String[] args) {
MinimumHeightTrees mht = new MinimumHeightTrees();
int n = 4;
int[][] edges = // use example above
System.out.println(mht.findMinHeightTrees(n, edges)); // Output: [1]
}
}
O(n)
, since there are n-1
edges.O(n)
.Overall, the time complexity is O(n)
, which is efficient for the input constraints (1 <= n <= 2 * 10^4
).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?