-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15-binary_tree_is_full.c
More file actions
81 lines (66 loc) · 1.22 KB
/
15-binary_tree_is_full.c
File metadata and controls
81 lines (66 loc) · 1.22 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "binary_trees.h"
/**
* size - frees a tree
* @tree:tree to be freed
* Return: size of tree
*/
int size(const binary_tree_t *tree)
{
int count = 1;
if (tree == NULL)
return (0);
count += size(tree->left);
count += size(tree->right);
return (count);
}
/**
* full - frees a tree
* @tree:tree to be freed
* Return:number of leaf nodes
*/
int full(const binary_tree_t *tree)
{
int count = 0;
if (tree == NULL)
return (0);
if (tree->left && tree->right)
count++;
count += full(tree->left);
count += full(tree->right);
return (count);
}
/**
* leaves - frees a tree
* @tree:tree to be freed
* Return:number of leaf nodes
*/
int leaves(const binary_tree_t *tree)
{
int count = 0;
if (tree == NULL)
return (0);
if (!tree->left && !tree->right)
count++;
count += leaves(tree->left);
count += leaves(tree->right);
return (count);
}
/**
* binary_tree_is_full - checks if a binary tree is full
* @tree:tree to measure
* Return: 1 if true else 0
*/
int binary_tree_is_full(const binary_tree_t *tree)
{
int total, leaf, fulls;
if (tree == NULL)
return (0);
total = size(tree);
leaf = leaves(tree);
fulls = full(tree);
if (leaf + fulls == total)
return (1);
else
return (0);
return (0);
}