-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path130-binary_tree_is_heap.c
More file actions
46 lines (43 loc) · 921 Bytes
/
130-binary_tree_is_heap.c
File metadata and controls
46 lines (43 loc) · 921 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
46
#include "binary_trees.h"
#include "102-binary_tree_is_complete.c"
/**
* check_max - goes through a binary tree cheking ropt as max value
* @tree: pointer to the root
* Return: 1 if all nodes are max, 0 in other cases
**/
int check_max(const binary_tree_t *tree)
{
int tmp1 = 1, tmp2 = 1;
if (!tree)
return (0);
if (!tree->left && !tree->right)
return (1);
if (tree->left)
{
if (tree->n <= tree->left->n)
return (0);
tmp1 = check_max(tree->left);
}
if (tree->right)
{
if (tree->n <= tree->right->n)
return (0);
tmp2 = check_max(tree->right);
}
return (tmp1 && tmp2);
}
/**
* binary_tree_is_heap - checks if a binary tree is heap
* @tree: pointer to the node
* Return: 1 in case BTS / 0 otherwise
**/
int binary_tree_is_heap(const binary_tree_t *tree)
{
int tmp;
if (!tree)
return (0);
tmp = binary_tree_is_complete(tree);
if (!tmp)
return (0);
return (check_max(tree));
}