-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeightBST.cpp
More file actions
44 lines (40 loc) · 929 Bytes
/
HeightBST.cpp
File metadata and controls
44 lines (40 loc) · 929 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
#include <iostream>
using namespace std;
struct Node {
int data;
Node*left;
Node*right;
};
Node* root = NULL;
struct Node* insert(struct Node* root, int x) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
temp->data = x;
temp->left = NULL;
temp->right = NULL;
if(root == NULL) {
root = temp;
}
else if(x <= root->data) {
root->left = insert(root->left, x);
} else {
root->right = insert(root->right, x);
}
return root;
}
int maxHeight(struct Node* root) {
if(root == NULL) {
return -1;
}
return max(maxHeight(root->left), maxHeight(root->right))+1;
}
int main() {
root = insert(root, 15);
root = insert(root,10);
root = insert(root,20);
root = insert(root,8);
root = insert(root,12);
root = insert(root,17);
root = insert(root,25);
int x = maxHeight(root);
cout<<x<<endl;
}