forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(LEETCODE)Min-Height-trees.cpp
77 lines (66 loc) · 1.97 KB
/
(LEETCODE)Min-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
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
77
class Solution {
public:
void dfs(int src, int par, vector<int> &depth, vector<vector<int>> &adj){
for(auto c: adj[src]){
if(c!=par){
depth[c]=depth[src]+1;
dfs(c, src, depth, adj);
}
}
}
void dfs2(int src, int par, vector<int> &parent, vector<int> &depth, vector<vector<int>> &adj){
for(auto c: adj[src]){
if(c!=par){
parent[c]=src;
depth[c]=depth[src]+1;
dfs2(c, src, parent, depth, adj);
}
}
}
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if(edges.size()==0){
return vector<int>{0};
}
vector<vector<int>> adj(n);
for(int i=0; i<edges.size(); i++){
adj[edges[i][0]].push_back(edges[i][1]);
adj[edges[i][1]].push_back(edges[i][0]);
}
vector<int> depth(n, 0);
dfs(0, -1, depth, adj);
int node1=-1, maxH=-1;
for(int i=0; i<n; i++){
if(depth[i]>maxH){
maxH=depth[i];
node1=i;
}
}
depth.clear();
depth.assign(n, 0);
vector<int> parent(n, -1);
dfs2(node1, -1, parent, depth, adj);
int node2=-1;
maxH=-1;
for(int i=0; i<n; i++){
if(depth[i]>maxH){
maxH=depth[i];
node2=i;
}
}
vector<int> diam;
int cur=node2;
while(cur!=node1){
diam.push_back(cur);
cur=parent[cur];
}
diam.push_back(node1);
vector<int> ans;
if(diam.size()%2){
ans.push_back(diam[diam.size()/2]);
}else{
ans.push_back(diam[diam.size()/2]);
ans.push_back(diam[(diam.size()/2) - 1]);
}
return ans;
}
};