# 拓扑排序变种题：Leetcode310最小高度树

### 代码：

class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
vector<int> ans;
if( n==1 )
{
ans.push_back(0);
return ans;
}
if( n==2 )
{
ans.push_back(0);
ans.push_back(1);
return ans;
}
int* indegree;
indegree=(int*)malloc( n*sizeof(int) );
int i;
for( i=0;i<n;i++ )
indegree[i]=0;
for( i=0;i<edges.size();i++ )
{
indegree[ edges[i][0] ]++;
indegree[ edges[i][1] ]++;
}
vector<vector<int>> table;
for( i=0;i<n;i++ )
table.push_back( vector<int>() );
for( i=0;i<edges.size();i++ )
{
table[ edges[i][1] ].push_back( edges[i][0] );
table[ edges[i][0] ].push_back( edges[i][1] );
}
queue<int> queue;
for( i=0;i<n;i++ )
{
if( indegree[i]==1 )
{
queue.push(i);
}
}
int len=queue.size();
int vertex;
while( n>2 )
{
n-=len;
while( len-- )
{
vertex=queue.front();
queue.pop();
indegree[vertex]=0;
for( i=0;i<table[vertex].size();i++ )
{
if( indegree[ table[vertex][i] ]!=0 )
{
if( --indegree[ table[vertex][i] ]==1 )
queue.push( table[vertex][i] );
}
}
}
len=queue.size();
}
while( queue.size()!=0 )
{
vertex=queue.front();
queue.pop();
ans.push_back(vertex);
}
return ans;
}
};


09-28 39万+

03-05 120

05-10 22

09-25 82

12-05 20

08-07 138

03-10 11

12-20 4746

08-25 240

02-19 1万+

12-07 2685

12-13 348

05-04 534

02-15 150

03-22 2万+

10-13 1万+

11-13 47

10-11 43

04-14 58万+

03-13 14万+

03-04 13万+

03-05 5671

03-08 4万+

03-08 7万+

03-10 12万+

03-12 11万+

03-13 11万+

03-14 9947

03-18 5407

03-18 8644

03-19 3万+

03-19 8万+

03-20 7414

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-29 21万+

03-29 1万+

03-29 10万+

03-30 16万+

05-28 5112

05-28 1万+

04-02 4万+

04-03 3058

04-06 7万+

04-07 5万+

04-09 8万+

04-09 2万+

05-17 7371

04-10 2462

04-11 3万+

04-15 5万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 5798

04-29 5582

04-30 4565

04-30 8813

04-30 8310

05-16 5万+

05-08 4万+

05-11 3万+

05-12 1万+

05-13 414

05-14 5606

05-14 1469

05-16 1万+

05-20 488

05-18 7609

05-27 503

05-18 2924

05-19 9758

05-21 5562

05-21 5571

05-25 3281

#### 前端很容易遇到的一个大坑

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客