-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-binary_tree_is_perfect.c
54 lines (45 loc) · 1.09 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
#include "binary_trees.h"
/**
* depth - a function that returns the depth of a tree node
* @tree: pointer to the tree node to check
* Return: integer otherwise 0
*/
int depth(const binary_tree_t *tree)
{
int dep = 0;
while (tree != NULL)
{
dep++;
tree = tree->left;
}
return (dep);
}
/**
* is_perfect - A function that checks id a binary tree is perfect
* @tree: pointer to a binary tree
* @d: depth of a binary tree
* @level: levels to check
* Return: 1 if perfect otherwise 0
*/
int is_perfect(const binary_tree_t *tree, int d, int level)
{
if (!tree)
return (0);
if (tree->left == NULL && tree->right == NULL)
return (d == level + 1);
if (tree->left == NULL || tree->right == NULL)
return (0);
return (is_perfect(tree->left, d, level + 1) &&
is_perfect(tree->right, d, level + 1));
}
/**
* binary_tree_is_perfect - A function that checks if a binary tree is perfect
* @tree: pointer to a binary tree
* Return: 1 if perfect otherwise 0
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int deep;
deep = depth(tree);
return (is_perfect(tree, deep, 0));
}