-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimumHeightTrees.cpp
56 lines (45 loc) · 1.28 KB
/
MinimumHeightTrees.cpp
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
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if (n == 1) {
return {0};
}
// Kahn
vector<int>* graph = new vector<int>[n];
int* degree = new int[n]();
for (int i = 0; i < edges.size(); i++) {
int a_i = edges[i][0];
int b_i = edges[i][1];
graph[a_i].push_back(b_i);
graph[b_i].push_back(a_i);
degree[a_i] += 1;
degree[b_i] += 1;
}
queue<int> leaves;
for (int i = 0; i < n; i++) {
if (degree[i] == 1) {
leaves.push(i);
}
}
while (n > 2) {
for (int _ = leaves.size(); _ > 0; _--) {
int v = leaves.front();
leaves.pop();
n--;
for (int i = 0; i < graph[v].size(); i++) {
int u = graph[v][i];
degree[u] -= 1;
if (degree[u] == 1) {
leaves.push(u);
}
}
}
}
vector<int> root;
while (!leaves.empty()) {
root.push_back(leaves.front());
leaves.pop();
}
return root;
}
};