-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary_tree_find_heigth.c
53 lines (46 loc) · 1.25 KB
/
binary_tree_find_heigth.c
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<stdlib.h>
#include<stdio.h>
struct TreeNode{ // node for the binary tree
int data;
struct TreeNode* left;
struct TreeNode* right;
};
struct TreeNode* GenerateNode(int data){ // generate new node
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct TreeNode* Insert(struct TreeNode* root,int data){ // insert new node using recursion
if(root == NULL){
root = GenerateNode(data);
return root;
}
if(data <= root->data){
root->left = Insert(root->left, data); // recursion call
} else {
root->right = Insert(root->right, data); // recursion call
}
return root;
}
int max(a, b){
if(a > b) return a;
return b;
}
int FindHeigth(struct TreeNode* root){
if(root == NULL){
return -1;
}
return max(FindHeigth(root->left), FindHeigth(root->right)) + 1;
}
// TEST>>>>NOT PART OF IMPLEMENTATION
int main(){
struct TreeNode* root = NULL;
root = Insert(root, 20);
root = Insert(root, 15);
root = Insert(root, 2);
root = Insert(root, 5);
root = Insert(root, 3);
printf("Height of tree is: %d\n", FindHeigth(root));
}