-
Notifications
You must be signed in to change notification settings - Fork 0
/
102-binary_tree_is_complete.c
51 lines (45 loc) · 1.06 KB
/
102-binary_tree_is_complete.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
44
45
46
47
48
49
50
51
#include "binary_trees.h"
/**
* binary_tree_is_complete - Checks if binary tree is complete
*
* @tree: Pointer to the root node of the tree to check
* Return: 0 if tree is NULL
*/
int binary_tree_is_complete(const binary_tree_t *tree)
{
size_t size;
if (!tree)
return (0);
size = binary_tree_size(tree);
return (btic_help(tree, 0, size));
}
/**
* btic_help - Checks if binary tree is complete
*
* @tree: Pointer to the root node of the tree to check
* @idx: Index
* @size: Size
* Return: if complete 1, otherwise NULL, 0
*/
int btic_help(const binary_tree_t *tree, size_t idx, size_t size)
{
if (!tree)
return (1);
if (idx >= size)
return (0);
return (btic_help(tree->left, 2 * idx + 1, size) && btic_help(
tree->right, 2 * idx + 2, size));
}
/**
* binary_tree_size - Measures the size of a binary tree
*
* @tree: Pointer to the root node to measure the size
* Return: 0 if tree is NULL
*/
size_t binary_tree_size(const binary_tree_t *tree)
{
if (!tree)
return (0);
return (binary_tree_size(tree->left) +
binary_tree_size(tree->right) + 1);
}