-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiamter.cpp
74 lines (63 loc) · 1.79 KB
/
diamter.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
//calculate diameter of the binary tree
//The diameter of a tree is the number of nodes on the longest path between two end nodes in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).
#include <bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *left;
node *right;
node(int x){
data = x;
left = NULL;
right = NULL;
}
};
node *insertLevelorder(node *root, int x){
node *temp = new node(x);
if(root == NULL){
root = temp;
return root;
}
queue<node *> q;
q.push(root);
while(!q.empty()){
node *cur = q.front(); q.pop();
if(cur->left != NULL) q.push(cur->left);
else{
cur->left = temp;
break;
}
if(cur->right != NULL) q.push(cur->right);
else{
cur->right = temp;
break;
}
}
return root;
}
//this function returns the height of the current tree and the longest diamter of the tree formed when the current node is root
pair<int, int> diameter(node *root){
if(root == NULL) return {0,0};
pair<int,int> lt = diameter(root->left);
pair<int, int> rt = diameter(root->right);
pair<int,int> my;
//assigning the height to the current tree
my.first = max(lt.first, rt.first) + 1;
//longest diameter is the maximum of either left diameter, right diamter or (nodes between deepest node on left subtree and the deepest node in the right subtree)
my.second = max(lt.first+rt.first+1, max(lt.second, lt.second));
return my;
}
int main(){
int n;
cin >> n;
node *root = NULL;
for(int i = 0; i < n; i++){
int x;
cin >> x;
root = insertLevelorder(root, x);
}
pair<int, int> ans = diameter(root);
cout << "diameter of the binary tree: " << ans.second << endl;
return 0;
}