-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path310.最小高度树.cpp
77 lines (72 loc) · 1.73 KB
/
310.最小高度树.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
/*
* @lc app=leetcode.cn id=310 lang=cpp
*
* [310] 最小高度树
*/
// @lc code=start
const int MaxN=3*1e4;
class Solution {
public:
int degrees[MaxN];
vector<vector<int>> e;
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
vector<int> ret;
// 特判
if(n==1)
{
ret.push_back(0);
return ret;
}
else if(n==2)
{
ret.push_back(0);
ret.push_back(1);
return ret;
}
memset(degrees,0,sizeof(degrees));
e.resize(n);
for(auto &it:edges)
{
degrees[it[0]]++;
degrees[it[1]]++;
e[it[0]].push_back(it[1]);
e[it[1]].push_back(it[0]);
}
queue<int> nodes;
for(int i=0;i<n;i++)
{
if(degrees[i]==1)
{
nodes.push(i);
degrees[i]--;
}
}
int currentTime=2;
while(!nodes.empty())
{// 如果nodes非空 不断循环找到下一层的内容放进去
int count=nodes.size();
ret.clear();
while(count--)
{
int i=nodes.front();
nodes.pop();
for(auto &it:e[i])
{
degrees[it]--;
// 如果成为了叶子节点
if(degrees[it]==1)
{
nodes.push(it);
// cout<<it<<endl;
}
}
ret.push_back(i);
}
currentTime++;
}
return ret;
}
};
// @lc code=end