-
Notifications
You must be signed in to change notification settings - Fork 0
/
9-binary_tree_height.c
41 lines (36 loc) · 1003 Bytes
/
9-binary_tree_height.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
#include "binary_trees.h"
size_t calculate_height(const binary_tree_t *tree, size_t height);
/**
* binary_tree_height - returns the height of the tree.
* @tree: The node that will act as root when calculating the height.
* Return: The height of the tree.
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t height;
if (tree == NULL)
return (0);
height = 0;
height = calculate_height(tree, height);
return (height - 1);
}
/**
* calculate_height - Calculates the height of a binary tree.
* @tree: The node that will act as root when calculating the height.
* @height: The actual height of the tree.
* Return: The height of the tree.
*/
size_t calculate_height(const binary_tree_t *tree, size_t height)
{
size_t left;
size_t right;
left = height;
right = height;
if (tree == NULL)
return (0);
left = left + calculate_height(tree->left, height);
right = right + calculate_height(tree->right, height);
if (left > right)
return (left + 1);
return (right + 1);
}