-
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
45 lines (40 loc) · 903 Bytes
/
16-binary_tree_is_perfect.c
File metadata and controls
45 lines (40 loc) · 903 Bytes
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
#include "binary_trees.h"
/**
* binary_tree_is_perfect - Check if a binary tree node is perfect
*
* @tree: Pointer to root node
*
* Return: 1 if perfect, otherwise 0 (int)
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
if (!tree)
return (0);
if (!(tree->left) && !(tree->right) && tree->parent)
{
if (height(tree->parent->left) != height(tree->parent->right))
return (0);
return (1);
}
if (!(tree->left) || !(tree->right))
return (0);
return (binary_tree_is_perfect(tree->left)
&& binary_tree_is_perfect(tree->right));
}
/**
* height - Subfunction used to avoid top level addition
* else top function would be enough :)
*
* @node: Node
*
* Return: 1 if node
*/
size_t height(const binary_tree_t *node)
{
if (!node)
return (0);
if (height(node->left) > height(node->right))
return (height(node->left) + 1);
else
return (height(node->right) + 1);
}