forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeight of BST
More file actions
53 lines (45 loc) · 995 Bytes
/
Height of BST
File metadata and controls
53 lines (45 loc) · 995 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
50
51
52
53
#include<iostream>
using namespace std;
// get the max of two no.s
int max(int a, int b) {
return ((a > b) ? a : b);
}
typedef struct node {
int value;
struct node *left, *right;
}node;
// create a new node
node *getNewNode(int value) {
node *new_node = new node;
new_node->value = value;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
// compute height of the tree
int getHeight(node *root) {
if (root == NULL)
return 0;
// find the height of each subtree
int lh = getHeight(root->left);
int rh = getHeight(root->right);
return 1 + max(lh,rh);
}
// create the tree
node *createTree() {
node *root = getNewNode(31);
root->left = getNewNode(16);
root->right = getNewNode(52);
root->left->left = getNewNode(7);
root->left->right = getNewNode(24);
root->left->right->left = getNewNode(19);
root->left->right->right = getNewNode(29);
return root;
}
// main
int main() {
node *root = createTree();
cout<<"\nHeight of the tree is "<<getHeight(root);
cout<<endl;
return 0;
}