-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-binary_tree_is_perfect.c
79 lines (58 loc) · 1.43 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
74
75
76
77
78
79
#include "binary_trees.h"
/**
* binary_tree_size - measures the size of a binary tree
* @tree: tree
* Return: size of tree
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
size_t left, right;
if (tree == NULL)
return (0);
left = binary_tree_size(tree->left);
right = binary_tree_size(tree->right);
return (left + right + 1);
}
/**
* binary_tree_height - measures the height of a binary tree
* @tree: tree
* Return: 0 or height of tree
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left, right;
if (tree == NULL)
return (0);
left = binary_tree_height(tree->left);
right = binary_tree_height(tree->right);
return (left > right ? left + 1 : right + 1);
}
/**
* binary_tree_is_full - checks if a binary tree is full
* @tree: tree
* Return: 1 or 0
*/
int binary_tree_is_full(const binary_tree_t *tree)
{
if (tree == NULL)
return (0);
if (tree->left == NULL && tree->right == NULL)
return (1);
if (tree->left != NULL && tree->right != NULL)
return (binary_tree_is_full(tree->left) && binary_tree_is_full(tree->right));
return (0);
}
/**
* binary_tree_is_perfect - checks if a binary tree is perfect
* @tree: tree
* Return: 0 or 1
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
size_t height, size;
if (tree == NULL)
return (0);
height = binary_tree_height(tree);
size = binary_tree_size(tree);
return (binary_tree_is_full(tree) && ((size_t)(1 << height) - 1 == size));
}