-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathdiameterBinaryTree.cpp
103 lines (82 loc) · 2.32 KB
/
diameterBinaryTree.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
using namespace std;
class node
{
public:
int data;
node* left, *right;
};
//function to calculate diameter of a binary tree
int calcDiameter(node *root, int* height){
if(root==NULL){
*height=-1;
return 0;
}
int leftHeight=0, rightHeight=0;
int leftDiameter=calcDiameter(root->left, &leftHeight);
int rightDiameter=calcDiameter(root->right, &rightHeight);
int currentDiameter = leftHeight + rightHeight + 2;
*height = std::max(leftHeight, rightHeight)+1;
return std::max( currentDiameter, std::max(leftDiameter, rightDiameter));
}
//function to create and set the new node
node* newNode(int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
// (1) <--root
// / \
// (2) (3)
// / \
// (4) (5)
// / \
// (6) (9)
// / \
// (7) (10)
// / \
// (8) (11)
// /
// (12)
/* Driver code*/
void inorderDisplay(node* root){
if(root==NULL) {
return;
}
inorderDisplay(root->left);
std::cout << root->data << " ";
inorderDisplay(root->right);
}
void postorderDisplay(node* root){
if(root==NULL) {
return;
}
postorderDisplay(root->left);
postorderDisplay(root->right);
std::cout << root->data << " ";
}
int main()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->left->right->left = newNode(6);
root->left->right->left->left = newNode(7);
root->left->right->left->left->left = newNode(8);
root->left->right->right = newNode(9);
root->left->right->right->right = newNode(10);
root->left->right->right->right->right = newNode(11);
root->left->right->right->right->right->left = newNode(12);
int height=0;
std::cout << "Inorder of Binary Tree: ";
inorderDisplay(root);
std::cout << "\nPostrder of Binary Tree: ";
postorderDisplay(root);
std::cout << "\nDiameter of the binary tree is: " << calcDiameter(root, &height) << "\n" ;
return 0;
}