-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-binary_tree_is_perfect.c
55 lines (49 loc) · 1.23 KB
/
16-binary_tree_is_perfect.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
54
55
#include "binary_trees.h"
/**
* tree_stats - computes some statistics about a binary tree.
* @tree: pointer to the root node of the tree.
* @n: accumulated height of the current path in the tree.
* @leaves: pointer to tree's leaf count.
* @height: pointer to tree's maximum height.
*
* Return: void
*/
void tree_stats(const binary_tree_t *tree, size_t n,
size_t *leaves, size_t *height)
{
if (tree != NULL)
{
if ((tree->left == NULL) && (tree->right == NULL))
{
if (leaves != NULL)
(*leaves)++;
if ((height != NULL) && (n > *height))
*height = n;
}
else
{
tree_stats(tree->left, n + 1, leaves, height);
tree_stats(tree->right, n + 1, leaves, height);
}
}
}
/**
* binary_tree_is_perfect - checks if a binary tree is perfect
* @tree: pointer to the root node of the tree to check
* Return: 1 if perfect
* 0 if not perfect or tree is NULL
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
size_t leaves_count = 0;
size_t tree_height = 0;
tree_stats(tree, 0, &leaves_count, &tree_height);
/**
* can also be written with one if statment
*
* return ((int)leaves_count == (1 << tree_height) ? 1 : 0);
*/
if ((int)leaves_count == (1 << tree_height))
return (1);
return (0);
}