-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16-binary_tree_is_perfect.c
More file actions
executable file
·77 lines (61 loc) · 1.67 KB
/
Copy path16-binary_tree_is_perfect.c
File metadata and controls
executable file
·77 lines (61 loc) · 1.67 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_is_perfect - Checks if a binary tree is perfect
*
* @tree: Pointer to the root node ofbthe tree to check
*
* Return: 1 if perfect, 0 otherwise and if tree is NULL
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int left_height = 0, right_height = 0;
if (tree == NULL)
return (0);
if (tree->left == NULL && tree->right == NULL)
return (1);
if (tree->left)
left_height = (int)binary_tree_height(tree->left);
if (tree->right)
right_height = (int)binary_tree_height(tree->right);
if (left_height == right_height)
{
if (binary_tree_is_perfect(tree->left)
&& binary_tree_is_perfect(tree->right))
return (1);
}
return (0);
}
/**
* binary_tree_height - Measures the height of a binary tree
*
* @tree: Pointer to the root node of the tree to measure the height
*
* Return: Height of the binary tree
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left_height = 0;
size_t right_height = 0;
if (tree == NULL)
return (0);
left_height = tree->left ? 1 + binary_tree_height(tree->left) : 0;
right_height = tree->right ? 1 + binary_tree_height(tree->right) : 0;
return (left_height > right_height ? left_height : right_height);
}
/**
* binary_tree_is_full - Checks if a binary tree is full
*
* @tree: Pointer to the root node of the tree to check
*
* Return: 1 if tree is full, 0 otherwise and if tree is NULL
*/
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 && tree->right)
return (binary_tree_is_full(tree->left) && binary_tree_is_full(tree->right));
return (0);
}