# C Program to Find the Height of Tree using Recursion

This is a C Program to find the Height of a Tree using Recursion.

Problem Description

We are given a tree, and we have to write a C program to find out the height of that tree using recursion. We have to create a recursive function which takes in root of the tree as input and returns height of the tree as output.

Expected Input and Output

Case 1. Tree having same weight on both the sides of root node (A Balanced Tree).
For example:

```If the input tree is
25
/    \
27     19
/ \     / \
17  91   13 55
the height of the tree here will be 3```

Case 2. Tree having only right children at every level (Right Skewed Tree). A right skewed tree is one in which all the nodes just have a right child at all the levels. For example:

``` If the input tree is
1
\
2
\
3
\
4
\
5
the height of the tree here will be 5```

Case 3. Tree having just one node. For example:

``` If the input tree is
15
the height of the tree here will be 1```
Problem Solution

We can easily find the height of the tree using recursion. We need to create a function which takes in root of the tree as parameter. After that we will compute the height of left subtree and right subtree and whichever is greater will be the maximum height of subtree.

Program/Source Code

Here is source code of the C Program to find the Height of a Tree using Recursion. The program is successfully compiled and tested using Codeblocks gnu/GCC compiler on windows 10. The program output is also shown below.

1. `/* C Program to find the height of a Tree */`
2. `#include <stdio.h>`
3. `#include <stdlib.h>`
4. ` `
5. `struct node`
6. `{`
7. `    int info;`
8. `    struct node* left, *right;`
9. `};`
10. ` `
11. `/*`
12. ` * Function to create new nodes`
13. ` */`
14. ` `
15. `struct node* createnode(int key)`
16. `{`
17. `    struct node* newnode = (struct node*)malloc(sizeof(struct node));`
18. `    newnode->info = key;`
19. `    newnode->left = NULL;`
20. `    newnode->right = NULL;`
21. ` `
22. `    return(newnode);`
23. `}`
24. ` `
25. `/*`
26. ` * Function to ascertain the height of a Tree`
27. ` */`
28. ` `
29. `int heightoftree(struct node* root)`
30. `{`
31. `    int max;`
32. ` `
33. `    if (root!=NULL)`
34. `    {`
35. `        /*Finding the height of left subtree.*/`
36. `        int leftsubtree = heightoftree(root->left); `
37. `        /*Finding the height of right subtree.*/`
38. `        int rightsubtree = heightoftree(root->right);  `
39. `        if (leftsubtree > rightsubtree)`
40. `        {`
41. `            max = leftsubtree + 1;`
42. `            return max;`
43. `        }`
44. `        else`
45. `        {`
46. `            max = rightsubtree + 1;`
47. `            return max;`
48. `        }`
49. `    }`
50. `}`
51. ` `
52. `/*`
53. ` * Main Function`
54. ` */`
55. ` `
56. `int main()`
57. `{`
58. `   /* Creating first Tree.*/`
59. ` `
60. `    struct node *newnode = createnode(25);`
61. `    newnode->left = createnode(27);`
62. `    newnode->right = createnode(19);`
63. `    newnode->left->left = createnode(17);`
64. `    newnode->left->right = createnode(91);`
65. `    newnode->right->left = createnode(13);`
66. `    newnode->right->right = createnode(55);`
67. ` `
68. `    /* Sample Tree 1- Balanced Tree`
69. ` `
70. ` `
71. `                    25`
72. `                  /    \`
73. `                 27     19`
74. `                / \     / \`
75. `              17  91   13 55`
76. ` `
77. ` `
78. `    */`
79. `    printf("Height of the first Tree is\t%d\n",heightoftree(newnode));`
80. ` `
81. `    /* Creating second Tree.*/`
82. ` `
83. `    struct node *node = createnode(1);`
84. `    node->right = createnode(2);`
85. `    node->right->right = createnode(3);`
86. `    node->right->right->right = createnode(4);`
87. `    node->right->right->right->right = createnode(5);`
88. ` `
89. `    /* Sample Tree 2-   Right Skewed Tree (Unbalanced).`
90. ` `
91. `                    1`
92. `                     \`
93. `                      2`
94. `                       \`
95. `                        3`
96. `                         \`
97. `                          4`
98. `                           \`
99. `                            5`
100. `    */`
101. ` `
102. `    printf("\nHeight of the second Tree is\t%d\n",heightoftree(node));`
103. ` `
104. `    /*Creating third Tree. */`
105. ` `
106. `    struct node *root = createnode(15);`
107. ` `
108. `    /* Sample Tree 3-  Tree having just one root node.`
109. ` `
110. `                   15`
111. ` `
112. `    */`
113. ` `
114. `    printf("\nHeight of the third Tree is\t%d",heightoftree(root));`
115. ` `
116. `    return 0;`
117. `}`
Program Explanation

1. We have created three trees, considering different cases. Tree can be balanced, unbalanced or one having just a single node.
2. Irrespective of the type of tree, the function heightoftree(struct node* root) will always return the correct height of the tree whether be it a balanced tree, unbalanced tree or one having a single node.
3. heightoftree(struct node* root) function takes in root of the tree as parameter. After passing root of the tree we have to check whether the tree exists or not.
4. If the root node of the tree is not NULL, only then we can compute the height of a tree.
5. We have to calculate maximum height of the subtree, for which we have called this function twice recursively by passing root->left then root->right.
6. heightoftree(root->left) will return the height of the left subtree, similarly heightoftree(root->right) will return height of the right subtree. After that we will add 1 to whichever of them is greater because in order to compute the total height of the tree, we need to consider the root node as well, that is why we have added 1.

Runtime Test Cases
```Height of the first Tree is     3

Height of the second Tree is    5

Height of the third Tree is     1```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms