-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
74 lines (67 loc) · 1.91 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @param {number} n
* @param {number[][]} edges
* @return {number[]}
*/
var findMinHeightTrees = function (n, edges) {
const depth = new Array(n).fill(0);
const graph = new Array(n);
const graph2 = new Array(n);
for (let i = 0; i < n; i++) {
graph[i] = [];
graph2[i] = [];
}
for (let i = 0; i < edges.length; i++) {
graph2[edges[i][0]].push(edges[i][1]);
graph2[edges[i][1]].push(edges[i][0]);
}
const used = new Array(n).fill(false);
used[0] = true;
const stack = [
0, ];
while (stack.length) {
const node = stack.pop();
for (let i = 0; i < graph2[node].length; i++) {
if (used[graph2[node][i]]) {
continue;
}
graph[node].push(graph2[node][i]);
stack.push(graph2[node][i]);
used[graph2[node][i]] = true;
}
}
function dfs (index) {
if (graph[index].length === 0) {
return 0;
}
const disList = graph[index].map((item) => dfs(item));
const maxDis = Math.max(...disList) + 1;
depth[index] = Math.max(maxDis, depth[index]);
for (let i = 0; i < disList.length; i++) {
let dis = 0;
for (let j = 0; j < disList.length; j++) {
if (i === j) {
continue;
}
dis = Math.max(dis, disList[j] + 1);
}
dfs2(graph[index][i], dis + 1);
}
return maxDis;
}
function dfs2 (index, dis) {
depth[index] = Math.max(depth[index], dis++);
for (let i = 0; i < graph[index].length; i++) {
dfs2(graph[index][i], dis);
}
}
dfs(0);
const minDepth = Math.min(...depth);
const result = [];
for (let i = 0; i < n; i++) {
if (depth[i] === minDepth) {
result.push(i);
}
}
return result;
};