-
Notifications
You must be signed in to change notification settings - Fork 617
/
diameter_of_binary_tree.cpp
109 lines (82 loc) · 2.57 KB
/
diameter_of_binary_tree.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
104
105
106
107
108
109
/*
Find the diameter of binary tree.
*/
//The diameter of a tree is the number of nodes on the largest path between two end nodes i,e the leaf nodes.
//Height is the number of nodes along the longest path from the root node down to the farthest node.
/*
Algorithm:
** Diameter of a tree is sum of the height of the left subtree and the right subtree.
1) We will calculate the height of left and right subtree by recursively calling the height function for every node.
2) Diameter of a tree is maximum diameter of -
A) Left subtree
B) Right subtree and
C) Height of left subtree + height of right subtree + 1
*/
#include<bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int val){
data = val;
//left and right child of node is initialised to NULL
left = NULL;
right = NULL;
}
};
//Recusive function to calculate the height of the tree
int height(struct Node* root){
//If the tree is empty height is 0
if(root==NULL)
return 0;
/*
Recusively calling height function to calculate the heigth of the tree
which is equal to 1 + max(height(root->left),height(root->right))
*/
return 1 + max(height(root->left), height(root->right));
}
//Recursive functin to calculate the diameter of the tree
int diameter(struct Node* root){
//base case if tree is NULL diameter is 0
if(root==NULL)
return 0;
//calculate the height of the left subtree
int leftHeight = height(root->left);
//calculate the height of the right subtree
int rightHeight = height(root->right);
//calcualte the diameter of the left subtree
int leftDiameter = diameter(root->left);
//calculate the diameter of the right subtree
int rightDiameter = diameter(root->right);
//Diameter of the tree
return max(leftHeight + rightHeight + 1 , max(leftDiameter, rightDiameter));
}
int main(){
/* Constructed binary tree is
1
/ \
2 3
/ \
4 5
*/
struct Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->right->left = new Node(4);
root->right->right = new Node(5);
cout<<"Diameter of the binary tree is : "<<diameter(root)<<endl;;
return 0;
}
/*
TestCase:
Constructed binary tree is
1
/ \
2 3
/ \
4 5
Output:
Diameter of the binary tree is : 4
Time Complexity : O(n^2) where n is the number of nodes in the binary tree
Space Complexity : O(1)
*/