-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-binary_tree_is_perfect.c
More file actions
executable file
·76 lines (57 loc) · 1.29 KB
/
Copy path16-binary_tree_is_perfect.c
File metadata and controls
executable file
·76 lines (57 loc) · 1.29 KB
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
#include "binary_trees.h"
/**
* binary_tree_height - measures the height of a tree
* @tree: pointer to the root node
* Return: if tree null 0, else the height
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
if (tree != NULL)
{
int count_left = 0;
int count_right = 0;
if (tree->left)
count_left = 1 + binary_tree_height(tree->left);
if (tree->right)
count_right = 1 + binary_tree_height(tree->right);
if (count_left < count_right)
return (count_right);
else
return (count_left);
}
return (0);
}
/**
*binary_tree_size - measures the size of a tree
*@tree: pointer to the root node of the tree
*Return: 0 if null, else the measure of the tree
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
int counter = 0;
if (tree != NULL)
{
counter++;
counter += binary_tree_size(tree->left);
counter += binary_tree_size(tree->right);
}
return (counter);
}
/**
*binary_tree_is_perfect - checks if a binary tree is perfect
*@tree: pointer to the root node
*
*Return: retunr 1 this a tree perfect else 0;
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int altura, tamanio;
int n = 1;
altura = binary_tree_height(tree);
tamanio = binary_tree_size(tree);
for (; altura >= 0; altura--)
{
n *= 2;
}
return (n - 1 == tamanio ? 1 : 0);
}