-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeightOfBst.cpp
More file actions
49 lines (49 loc) · 928 Bytes
/
HeightOfBst.cpp
File metadata and controls
49 lines (49 loc) · 928 Bytes
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
#include<iostream>
using namespace std;
struct Node{
int data;
Node *left;
Node *right;
};
Node *root=NULL;
Node* Create(int data){
Node *newNode=new Node();
newNode->data=data;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}
int Height(Node *temp)
{
if(root=NULL)
{
return 0;
}
else
{
int leftH=0,rightH=0;
if(temp->left!=NULL)
{
leftH=Height(temp->left);
}
if(temp->right!=NULL)
{
rightH=Height(temp->right);
}
int max=(leftH>rightH)?leftH:rightH;
return max+1;
}
}
int main()
{
root = Create(1);
root->left = Create(2);
root->right = Create(3);
root->left->left = Create(4);
root->right->left = Create(5);
root->right->right = Create(6);
root->right->right->right= Create(7);
root->right->right->right->right = Create(8);
cout<<"Maximum Height is : "<<Height(root);
return 0;
}