-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0310.MinimumHeightTrees.cpp
More file actions
49 lines (45 loc) · 1.21 KB
/
0310.MinimumHeightTrees.cpp
File metadata and controls
49 lines (45 loc) · 1.21 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
39
40
41
42
43
44
45
46
47
48
49
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
// Place edges into set.
set<pair<int, int>> binEdges;
for (int i = 0; i < edges.size(); i++)
binEdges.emplace(edges[i][0], edges[i][1]);
// Calculate node totals + edges.
vector<bool> isCommon(n, true);
vector<int> totals(n, 0);
for (const auto& edge : binEdges) {
// Update total.
totals[edge.first]++;
totals[edge.second]++;
}
bool hasRemoved = true;
while (binEdges.size() > 1 && hasRemoved) {
// Update commons.
for (int i = 0; i < totals.size(); i++)
isCommon[i] = totals[i] > 1;
// Remove leafs.
hasRemoved = false;
for (auto it = binEdges.begin(); it != binEdges.end();) {
// Check if leaf.
if ((totals[it->first] == 1 && !isCommon[it->first]) ||
(totals[it->second] == 1 && !isCommon[it->second])) {
// Decrement.
totals[it->first]--;
totals[it->second]--;
// Remove.
it = binEdges.erase(it);
// Update details.
hasRemoved = true;
} else {
it++;
}
}
}
vector<int> remainingNodes;
for (int i = 0; i < totals.size(); i++)
if (isCommon[i])
remainingNodes.emplace_back(i);
return remainingNodes;
}
};