-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path310.cpp
More file actions
38 lines (36 loc) · 1.06 KB
/
310.cpp
File metadata and controls
38 lines (36 loc) · 1.06 KB
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>> graph(n);
vector<int> inDegrees(n, 0);
for (auto& edge : edges) {
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
inDegrees[edge[0]]++;
inDegrees[edge[1]]++;
}
queue<int> q;
for (int i = 0; i < n; ++i) {
if (inDegrees[i] == 1) q.push(i);
}
while (n > 2) {
int size = q.size();
n -= size;
for (int i = 0; i < size; ++i) {
int node = q.front();
q.pop();
for (auto neighbor : graph[node]) {
inDegrees[neighbor]--;
if (inDegrees[neighbor] == 1) q.push(neighbor);
}
}
}
vector<int> res;
while (!q.empty()) {
res.push_back(q.front());
q.pop();
}
return res;
}
};