-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path16-binary_tree_is_perfect.c
executable file
·68 lines (52 loc) · 1.06 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
63
64
65
66
67
#include <stdlib.h>
#include "binary_trees.h"
/**
* 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 else 0
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int d;
if (!tree)
return (0);
d = depth(tree);
return (is_perfect(tree, d, 0));
}
/**
* depth - Calculate the depth
* @tree: Pointer to root
*
* Return: The calculate of the depth
*/
int depth(const binary_tree_t *tree)
{
int d = 0;
binary_tree_t *node = tree->left;
if (tree)
d++;
while (node)
{
d++;
node = node->left;
}
return (d);
}
/**
* is_perfect - Check if the tree is perfect
* @tree: Pointer to root
* @depth: The depth
* @level: The level
*
* Return: 1 if the root is perfect else 0
*/
int is_perfect(const binary_tree_t *tree, int depth, int level)
{
if (!tree->left && !tree->right)
return (depth == level + 1);
if (!tree->left || !tree->right)
return (0);
return (is_perfect(tree->left, depth, level + 1) &&
is_perfect(tree->right, depth, level + 1));
}