-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-binary_tree_is_perfect.c
72 lines (61 loc) · 1.67 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
#include "binary_trees.h"
/**
* binary_tree_is_perfect - This function checks if a binary tree is perfect
* @tree: A pointer to the root node
* Return: 1 if a tree is perfect, otherwise 0
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
/* empty tree is APPARENTLY NOT perfect */
if (!tree)
return (0);
/* single node is perfect */
if (binary_tree_is_leaf(tree))
return (1);
/* if left & right-child exist */
if (tree->left && tree->right)
{
/* check depth */
if (binary_tree_height(tree->left) != binary_tree_height(tree->right))
return (0);
}
/* recursively check depth of subtrees */
if (binary_tree_is_perfect(tree->left) == 0 ||
binary_tree_is_perfect(tree->right) == 0)
return (0);
return (1);
}
/**
* binary_tree_height - This function measures the height of a binary tree
* @tree: A pointer to the root node
* Return: Height of the tree, or 0 if tree is NULL
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left_depth = 0, right_depth = 0;
if (!tree || (!tree->left && !tree->right))
return (0);
/* compute the depth of each subtree if they exist */
left_depth = binary_tree_height(tree->left);
right_depth = binary_tree_height(tree->right);
/* compare the values of each depth */
if (left_depth >= right_depth)
return (left_depth + 1);
else
return (right_depth + 1);
}
/**
* binary_tree_is_leaf - This function checks if a node is a leaf
* @node: A pointer to the node
* Return: 1 if node is a leaf, otherwise 0
*/
int binary_tree_is_leaf(const binary_tree_t *node)
{
if (node)
{
/* if left & right child doesn't exist--it's a leaf! */
if (!node->left && !node->right)
return (1);
}
return (0);
}