-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-binary_tree_is_perfect.c
44 lines (36 loc) · 1.07 KB
/
16-binary_tree_is_perfect.c
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
#include "binary_trees.h"
/**
* dif_height_lr_nodes - Finds the height difference of left and right nodes
* and number of nodes in a binary tree
* @tree: pointer to the root node of the tree to check
* @num_nodes: pointer to number of nodes
* Return: height difference of left and right nodes
*/
int dif_height_lr_nodes(const binary_tree_t *tree, int *num_nodes)
{
int lht, rht;
if (!tree)
return (0);
(*num_nodes)++;
lht = tree->left ? 1 + dif_height_lr_nodes(tree->left, num_nodes) : 0;
rht = tree->right ? 1 + dif_height_lr_nodes(tree->right, num_nodes) : 0;
return (lht - rht);
}
/**
* binary_tree_is_perfect - checks if a binary tree is perfect
* @tree: pointer to the node of the tree to check
*
* Return: if tree is NULL, 0
*/
int binary_tree_is_perfect(const binary_tree_t *tree)
{
int lht, rht, l_num_nodes = 0, r_num_nodes = 0;
if (!tree)
return (0);
/* ht means height*/
lht = dif_height_lr_nodes(tree->left, &l_num_nodes);
rht = dif_height_lr_nodes(tree->right, &r_num_nodes);
if (lht == rht && l_num_nodes == r_num_nodes)
return (1);
return (0);
}