-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTree_Height.cpp
54 lines (51 loc) · 1.19 KB
/
Tree_Height.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
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int Data;
node* left;
node* right;
//this is a pointer to active class object
node(int d){
this->Data=d;
this->left=NULL;
this->right=NULL;
}
};
//Creation
node* createTree(node* root){
cout<<"\n DATA: ";
int data;
cin>>data;
if(data==-1){
return root;
}
root= new node(data);
cout<<"Left of "<<data;
root->left=createTree(root->left);
cout<<"Right of "<<data;
root->right=createTree(root->right);
}
//Algorith: Tree HEIGHT and WIDTH
int height(struct node* root){
if(root==NULL)
return 0;
int Hl=height(root->left),Hr=height(root->right);
if(Hl>=Hr)
return Hl+1;
else return Hr+1;
}
// Function to return the diameter of a Binary Tree.
int diameter(struct node* root) {
if(root==NULL)
return 0;
int dia=height(root->left)+height(root->right)+1;
int diaL=diameter(root->left),diaR=diameter(root->right);
if(dia>=max(diaL,diaR))
return dia;
else return max(diaL,diaR);
}
int main(){
node* root=createTree(root);
cout<<diameter(root);
}