-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
38 lines (38 loc) · 983 Bytes
/
Solution.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
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if (n == 1) {
return {0};
}
vector<vector<int>> g(n);
vector<int> degree(n);
for (auto& e : edges) {
int a = e[0], b = e[1];
g[a].push_back(b);
g[b].push_back(a);
++degree[a];
++degree[b];
}
queue<int> q;
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.push(i);
}
}
vector<int> ans;
while (!q.empty()) {
ans.clear();
for (int i = q.size(); i > 0; --i) {
int a = q.front();
q.pop();
ans.push_back(a);
for (int b : g[a]) {
if (--degree[b] == 1) {
q.push(b);
}
}
}
}
return ans;
}
};