-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-binary_tree_is_perfect.c
73 lines (56 loc) · 1.5 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
68
69
70
71
72
73
#include "binary_trees.h"
/**
* is_perfect - checks if a binary tree is perfect
* @node: current node being checked
* @level: the level of the target node
* @height: height of the binary tree
*
* Return: 1 if the binary tree is perfect, 0 otherwise
*/
int is_perfect(const binary_tree_t *node, int level, int height)
{
if (node == NULL)
return (1);
if (node->left == NULL && node->right == NULL)
{
return (level == height);
}
if (node->left != NULL && node->right != NULL)
{
return ((is_perfect(node->left, level + 1, height)
&& (is_perfect(node->right, level + 1, height))));
}
return (0);
}
/**
* binary_tree_height - measures the height of a binary tree
* @tree: pointer to the root node of the target tree
*
* Return: 0 if the tree is NULL, tree height on success
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left = 0, right = 0;
if (tree == NULL)
{
return (0);
}
left = tree->left ? binary_tree_height(tree->left) + 1 : 0;
right = tree->right ? binary_tree_height(tree->right) + 1 : 0;
return ((left > right) ? left : right);
}
/**
* binary_tree_is_perfect - checks if a binary tree is perfect(each
* node has exactly 2 children and all leaf nodes are at same level)
* @tree: pointer to the root node of the target tree
*
* Return: 1 on success, 0 otherwise
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int height = 0;
if (tree == NULL)
return (0);
height = binary_tree_height(tree);
return (is_perfect(tree, 0, height));
}