-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminimum-height-trees.cpp
42 lines (38 loc) · 979 Bytes
/
minimum-height-trees.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
typedef vector<int> vi;
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if(n == 1)
return {0};
vi adj[n];
vi indeg(n,0);
for(auto& e: edges){
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
indeg[e[0]]++;
indeg[e[1]]++;
}
// topo - kahn's
queue<int> q;
for (int i = 0; i < n; ++i){
if(indeg[i] == 1)
q.push(i);
}
vi ans;
while(!q.empty()){
int sz = q.size();
ans.clear();
while(sz--){
int curr = q.front();
q.pop();
ans.push_back(curr);
for(auto v: adj[curr]){
indeg[v]--;
if(indeg[v] == 1)
q.push(v);
}
}
}
return ans;
}
};