-
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
53 lines (44 loc) · 1.1 KB
/
Copy path16-binary_tree_is_perfect.c
File metadata and controls
53 lines (44 loc) · 1.1 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
#include "binary_trees.h"
/**
*binary_tree_height - This function measures the height of a binary tree
*
*@tree: Pointer to the root node of the tree to measure the height
*Return: nothing
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t left = 0;
size_t right = 0;
if (!tree || (!tree->left && !tree->right))
return (0);
if (tree->left)
left = binary_tree_height(tree->left);
if (tree->right)
right = binary_tree_height(tree->right);
if (left > right)
return (left + 1);
else
return (right + 1);
}
/**
* binary_tree_is_perfect - A function to check for is perfection
* @tree: a pointer to the root node of the tree to check
*
* Return:0 If tree is NULL
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int left_h, right_h;
if (tree == NULL)
return (0);
left_h = binary_tree_height(tree->left);
right_h = binary_tree_height(tree->right);
if (left_h == right_h)
{
if (tree->left == NULL && tree->right == NULL)
return (1);
return (binary_tree_is_perfect(tree->left) &&
binary_tree_is_perfect(tree->right));
}
return (0);
}