-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path14-binary_tree_balance.c
More file actions
33 lines (29 loc) · 885 Bytes
/
14-binary_tree_balance.c
File metadata and controls
33 lines (29 loc) · 885 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
#include "binary_trees.h"
/**
* binary_tree_balance - Measures balance factor of a binary tree.
* @tree: A pointer to root node of the tree to measure the balance factor.
* Return: If tree is NULL, return 0, else return balance factor.
*/
int binary_tree_balance(const binary_tree_t *tree)
{
if (tree)
return (binary_tree_height(tree->left) - binary_tree_height(tree->right));
return (0);
}
/**
* binary_tree_height - Measures the height of a binary tree.
* @tree: A pointer to the root node of the tree to measure the height.
*
* Return: If tree is NULL, your function must return 0, else return height.
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
if (tree)
{
size_t l = 0, r = 0;
l = tree->left ? 1 + binary_tree_height(tree->left) : 1;
r = tree->right ? 1 + binary_tree_height(tree->right) : 1;
return ((l > r) ? l : r);
}
return (0);
}