-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-binary_tree_is_perfect.c
62 lines (52 loc) · 1.33 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
56
57
58
59
60
61
62
#include "binary_trees.h"
/**
* height - Measure the height of a binary tree.
*
* @tree: Pointer to the root node of the tree.
*
* Return: The height of a binary tree.
*/
size_t height(const binary_tree_t *tree)
{
size_t left, right;
if (!tree)
return (0);
left = tree->left ? 1 + height(tree->left) : 0;
right = tree->right ? 1 + height(tree->right) : 0;
return (left > right ? left : right);
}
/**
* is_perfect - recursive function to check if a binary tree is perfect.
*
* @tree: Pointer to the root node of the tree to check.
* @currH: The current height of a binary tree.
* @actualH: The actual height of a binary tree.
*
* Return: 1 if perfect and 0 otherwise.
*/
int is_perfect(const binary_tree_t *tree, size_t currH, size_t actualH)
{
if (!tree)
return (0);
if (!tree->left && !tree->right)
return (currH == actualH);
if (!tree->left || !tree->right)
return (0);
return (is_perfect(tree->left, currH + 1, actualH) &&
is_perfect(tree->right, currH + 1, actualH));
}
/**
* 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 tree is perfect and 0 otherwise.
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
size_t actualH;
if (!tree)
return (0);
actualH = height(tree);
return (is_perfect(tree, 0, actualH));
}