310. Minimum Height Trees
Medium
For an undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.
Format
The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
Example 1 :
Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]
0
|
1
/ \
2 3
Output: [1]
Example 2 :
Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
0 1 2
\ | /
3
|
4
|
5
Output: [3, 4]
Note:
According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
题意
给定一个无向图的边列表,该无向图有n个节点和n-1条边,符合树的定义,要在这个无向图可以组成的所有树中,求使得树根到最远的树叶距离最短的树根节点的列表(可能有2个树根同时满足最短)
思路
依次删除度为1的节点,直到最后只剩下2个及以下的节点。
注意Java List的浅拷贝(每次while循环rem0浅拷贝自rem1),如果直接赋值(rem0 = rem1)则rem1改变rem0也会改变。一般来说List的浅拷贝可以采用从构造方法构造
List<T> rem0 = new List<T>(rem1);
或者先清空再使用list的addAll方法
rem0.clear();
rem0.addAll(rem1);
代码
class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> ans = new ArrayList<Integer>();
int i = 0;
if (n <= 2) {
for (i=0; i<n; ++i) {
ans.add(i);
}
return ans;
}
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>(n);
int[] degree = new int[n];
ArrayList<Integer> rem0 = new ArrayList<Integer>(), rem1 = new ArrayList<Integer>();
boolean[] vis = new boolean[n];
int cnt = n;
for (i=0; i<n; ++i) {
adj.add(new ArrayList<Integer>());
}
for (int[] edge: edges) {
adj.get(edge[0]).add(edge[1]);
adj.get(edge[1]).add(edge[0]);
++degree[edge[0]];
++degree[edge[1]];
}
for (i=0; i<n; ++i) {
if (degree[i] == 1) {
rem0.add(i);
vis[i] = true;
--cnt;
}
}
while (cnt > 2) {
rem1.clear();
for (int rem: rem0) {
for (int node: adj.get(rem)) {
--degree[node];
if (!vis[node] && degree[node] == 1) {
rem1.add(node);
vis[node] = true;
--cnt;
}
}
}
rem0.clear();
rem0.addAll(rem1);
}
for (i=0; i<n; ++i) {
if (!vis[i]) {
ans.add(i);
}
}
return ans;
}
}