-
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
40 lines (37 loc) · 949 Bytes
/
16-binary_tree_is_perfect.c
File metadata and controls
40 lines (37 loc) · 949 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
#include "binary_trees.h"
/**
* binary_tree_is_perfect - checks perfection
* Return: 1 for perfect, else 0
* @tree: the root node to check
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int left_p, right_p;
if (!tree)
return (0);
if (!(tree->left) && !(tree->right))
return (1);
if (binary_tree_height(tree->left) != binary_tree_height(tree->right))
return (0);
left_p = binary_tree_is_perfect(tree->left);
right_p = binary_tree_is_perfect(tree->right);
return (left_p && right_p);
}
/**
* binary_tree_height - finds the height of the binary tree
* Return: the height as a size_t
* @tree: the root node
*/
size_t binary_tree_height(const binary_tree_t *tree)
{
size_t h_left, h_right;
if (!tree)
return (0);
if (!(tree->left) && !(tree->right))
return (0);
h_left = binary_tree_height(tree->left);
h_right = binary_tree_height(tree->right);
if (h_left > h_right)
return (1 + h_left);
return (1 + h_right);
}