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.
题目链接:https://leetcode.com/problems/minimum-height-trees/
题目分析:答案显然是树的直径(树上最长边)的中点,可以利用反证法,假设答案为v且其不是直径u的中点,那么从v点到其离直径较远的端点的路径距离必然比直径中点到该端点的距离远(仿佛是废话),求直径利用两次DFS
8ms,时间击败99.19%,空间击败94.44%
讨论区比较常见的解法是用BFS类似拓扑排序的方式,像剥洋葱一样,从外向内一层一层删
class Solution {
class EDGE {
public EDGE() {}
int to, nxt;
}
class Graph {
int n, cnt;
int[] head;
EDGE[] e;
Graph(int n) {
this.n = n;
this.cnt = 0;
this.head = new int[n];
this.e = new EDGE[n << 1];
Arrays.fill(this.head, -1);
}
void addEdge(int u, int v) {
this.e[cnt].to = v;
this.e[cnt].nxt = head[u];
this.head[u] = cnt++;
}
void build(int[][] edges) {
for (int i = 0; i < edges.length << 1; i++) {
this.e[i] = new EDGE();
}
for (int i = 0; i < edges.length; i++) {
this.addEdge(edges[i][0], edges[i][1]);
this.addEdge(edges[i][1], edges[i][0]);
}
}
}
void DFS(int u, int fa, Graph g, int[] dis) {
for (int i = g.head[u]; i != -1; i = g.e[i].nxt) {
int v = g.e[i].to;
if (v != fa) {
dis[v] = dis[u] + 1;
DFS(v, u, g, dis);
}
}
}
boolean flag = false;
void getPath(int u, int fa, int ed, Graph g, int[] path) {
if (flag) {
return;
}
if (u == ed) {
flag = true;
return;
}
for (int i = g.head[u]; i != -1; i = g.e[i].nxt) {
int v = g.e[i].to;
if (v != fa) {
path[v] = u;
getPath(v, u, ed, g, path);
}
}
}
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> ans = new ArrayList<>();
Graph g = new Graph(n);
g.build(edges);
int[] dis = new int[n];
// find start node of the diameter
DFS(0, -1, g, dis);
int st = 0, ed = 0, ma = 0;
for (int i = 0; i < n; i++) {
if (dis[i] > ma) {
ma = dis[i];
st = i;
}
}
// find end node of the diameter
Arrays.fill(dis, 0);
DFS(st, -1, g, dis);
ma = 0;
for (int i = 0; i < n; i ++) {
if (dis[i] > ma) {
ma = dis[i];
ed = i;
}
}
// get path of the diameter
int[] path = new int[n];
path[st] = -1;
getPath(st, -1, ed, g, path);
int[] tmp = new int[n];
int num = 0, pos = ed;
while (pos != -1) {
tmp[num++] = pos;
pos = path[pos];
}
// get middle of diameter
ans.add(tmp[ma / 2]);
if (ma % 2 == 1) {
ans.add(tmp[ma / 2 + 1]);
}
return ans;
}
}