-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Diameter.cpp
More file actions
57 lines (47 loc) · 1.04 KB
/
Tree_Diameter.cpp
File metadata and controls
57 lines (47 loc) · 1.04 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
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
#define forn(i,k,e) for(int i = k; i < e; i++)
/*
Find max depth from each child of current node
add top 2 max depths + 1 = to get the longest path containing current node
repeat recursively.
TC = O(N) tree visited only once
SC = O(1) no extra space to store tree data
*/
signed main() {
int n;
cin >> n;
map<int, vector<int>> adj;
forn(i, 1, n) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int ans = 0;
auto dfs = [&](auto && self, int cur, int parent) -> int {
int d1 = 0, d2 = 0;
if (adj[cur].size() == 1 && adj[cur][0] == parent) {
//leaf node - return dist from parent node to cur node = 1
return 1;
}
for (int nxt : adj[cur]) {
if (nxt != parent) {
int dist = self(self, nxt, cur);
if (dist > d1) {
d2 = d1;
d1 = dist;
} else if (dist > d2) d2 = dist;
}
ans = max(ans, 1 + d1 + d2);
}
return 1 + d1;
};
dfs(dfs, adj.begin()->first, -1);
if (n == 1) {
cout << 0;
} else {
cout << ans - 1;
}
return 0;
}