个人认为这是一道挺难的题,本来以为是考察DFS、BFS的用法,因此一直构思如何遍历所有点并求出相应的树的高度并找出最小值,但写了很久发现还是写不出来。最后只能上网看一下这道题的思路,才知道这题重点考察图的运用。
这道题的算法思想是:先用二维向量构造出图,找出所有叶子结点,而连接这些叶子结点的父节点也要相应剪掉这些叶子结点;而当这些父节点变成叶子结点的时候,同样进行上述步骤。重复以上过程,直到只剩下不多于两个节点的根节点。
虽然算法乍看下来还是很简单的,但是实现起来我还是才了不少坑(C++学艺不精的结果)。一开始我用的是vector+list来存储图的,在循环遍历删除叶子结点时一直报错,结果发现是for循环中使用iterator++会导致迭代器指向错误;更改之后仍然在同一位置报错,仔细查看原来是iterator.erase()会使原迭代器失效,因此需要将iterator.erase()返回的迭代器赋值给原迭代器才可以。顺利debug之后的代码如下,但是TLE:
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
if (edges.size() == 0) {
vector<int> vec;
vec.push_back(0);
return vec;
}
vector<list<int>> graph(n, list<int>());
queue<int> q;
list<int>::iterator it;
vector<int> res;
int count = 0;
for (auto t : edges) {
graph[t.first].push_back(t.second);
graph[t.second].push_back(t.first);
}
for (int i = 0; i < graph.size(); i++) {
if (graph[i].size() == 1) {
q.push(i);
}
}
while (n - 2 > count) {
int times = q.size();
while (times--) {
int tmp = q.front();
int i;
for (i = 0; i < graph.size(); i++) {
if (tmp != i) {
for (it = graph[i].begin(); it != graph[i].end(); it) {
if (tmp == *it) {
it = graph[i].erase(it);
if (graph[i].size() == 1) {
q.push(i);
}
}
else
it++;
}
}
else
graph[i].erase(graph[i].begin());
}
q.pop();
count++;
}
}
while (!q.empty()) {
res.push_back(q.front());
q.pop();
}
return res;
}
};
后来查资料发现list.size()的时间复杂度为O(n),因此使用这个函数会耗费太多时间,于是重新使用vector+set来作出图,但仍然是TLE:
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
if (edges.size() == 0) {
vector<int> vec;
vec.push_back(0);
return vec;
}
vector<set<int>> graph(n, set<int>());
queue<int> q;
set<int>::iterator it;
vector<int> res;
int count = 0;
for (auto t : edges) {
graph[t.first].insert(t.second);
graph[t.second].insert(t.first);
}
for (int i = 0; i < graph.size(); i++) {
if (graph[i].size() == 1) {
q.push(i);
}
}
while (n - 2 > count) {
int times = q.size();
while (times--) {
int tmp = q.front();
int i;
for (i = 0; i < graph.size(); i++) {
if (tmp != i) {
for (it = graph[i].begin(); it != graph[i].end(); it) {
if (tmp == *it) {
it = graph[i].erase(it);
if (graph[i].size() == 1) {
q.push(i);
}
}
else
it++;
}
}
else
graph[i].erase(graph[i].begin());
}
q.pop();
count++;
}
}
while (!q.empty()) {
res.push_back(q.front());
q.pop();
}
return res;
}
};
百思不得其解,最后还是去网上参考了别人的做法,发现erase时根本不需要两重循环,活用图能够极快的完成删除操作:
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
if (edges.size() == 0) {
vector<int> vec;
vec.push_back(0);
return vec;
}
vector<set<int>> graph(n, set<int>());
queue<int> q;
set<int>::iterator it;
vector<int> res;
int count = 0;
for (auto t : edges) {
graph[t.first].insert(t.second);
graph[t.second].insert(t.first);
}
for (int i = 0; i < graph.size(); i++) {
if (graph[i].size() == 1) {
q.push(i);
}
}
while (n - 2 > count) {
int times = q.size();
while (times--) {
int tmp = q.front();
for (auto t : graph[tmp]) {
graph[t].erase(tmp);
if (graph[t].size() == 1) {
q.push(t);
}
}
q.pop();
count++;
}
}
while (!q.empty()) {
res.push_back(q.front());
q.pop();
}
return res;
}
};
由于图能清楚快速找到某个元素所连接的其他节点,因此可以找出要删除节点所连接的其他节点,并且set能通过迭代器也能根据元素直接删除的特性,从图找出其他节点的set集合再快速erase需要删除的节点,能大量降低复杂度,可以轻松AC。
总的来说,这道题收获还是很大的,除了对图有了更深的理解之外,也了解了STL各种容器之间的差异以及自己对其认识的不足,更能加深对各个STL容器的理解,对今后的学习非常有帮助。