-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree_diameter_DFS.cpp
More file actions
88 lines (74 loc) · 1.94 KB
/
Copy pathtree_diameter_DFS.cpp
File metadata and controls
88 lines (74 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// C++ program to find diameter of a binary tree
// using DFS.
#include <iostream>
#include <limits.h>
#include <list>
using namespace std;
// Used to track farthest node.
int x;
// Sets maxCount as maximum distance from node.
void dfsUtil(int node, int count, bool visited[],
int& maxCount, list<int>* adj)
{
visited[node] = true;
count++;
for (auto i = adj[node].begin(); i != adj[node].end(); ++i) {
if (!visited[*i]) {
if (count >= maxCount) {
maxCount = count;
x = *i;
}
dfsUtil(*i, count, visited, maxCount, adj);
}
}
}
// The function to do DFS traversal. It uses recursive
// dfsUtil()
void dfs(int node, int n, list<int>* adj, int& maxCount)
{
bool visited[n + 1];
int count = 0;
// Mark all the vertices as not visited
for (int i = 1; i <= n; ++i)
visited[i] = false;
// Increment count by 1 for visited node
dfsUtil(node, count + 1, visited, maxCount, adj);
}
// Returns diameter of binary tree represented
// as adjacency list.
int diameter(list<int>* adj, int n)
{
int maxCount = INT_MIN;
/* DFS from a random node and then see
farthest node X from it*/
dfs(1, n, adj, maxCount);
/* DFS from X and check the farthest node
from it */
dfs(x, n, adj, maxCount);
return maxCount;
}
/* Driver program to test above functions*/
int main()
{
int n = 5;
/* Constructed tree is
1
/ \
2 3
/ \
4 5 */
list<int>* adj = new list<int>[n + 1];
/*create undirected edges */
adj[1].push_back(2);
adj[2].push_back(1);
adj[1].push_back(3);
adj[3].push_back(1);
adj[2].push_back(4);
adj[4].push_back(2);
adj[2].push_back(5);
adj[5].push_back(2);
/* maxCount will have diameter of tree */
cout << "Diameter of the given tree is "
<< diameter(adj, n) << endl;
return 0;
}