-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path110-binary_tree_is_bst.c
More file actions
53 lines (46 loc) · 1.12 KB
/
110-binary_tree_is_bst.c
File metadata and controls
53 lines (46 loc) · 1.12 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
#include "binary_trees.h"
int lr_most(binary_tree_t *tree, int rl);
/**
*binary_tree_is_bst - This function goes through a binary tree using
*in-order traversal to find if the binary tree is a Binary Search Tree
*
*@tree: Pointer to the root node of the tree to traverse
*Return: nothing
*/
int binary_tree_is_bst(const binary_tree_t *tree)
{
int left = 1;
int right = 1;
int c_left = 1;
int c_right = 1;
if (!tree)
return (0);
if (tree->left)
{
left = binary_tree_is_bst(tree->left);
c_left = lr_most(tree->left, 0) < tree->n;
}
if (tree->right)
{
right = binary_tree_is_bst(tree->right);
c_right = lr_most(tree->right, 1) > tree->n;
}
return (left && c_left && c_right && right);
}
/**
*lr_most - last left or right most element down a tree
*@tree: Pointer to the root node of the tree to check
*@rl: Integer representing 0 for left most and 1 for right most element
*Return: Value of the node at the last element
*/
int lr_most(binary_tree_t *tree, int rl)
{
binary_tree_t *h = tree;
if (rl == 0)
while (h->right)
h = h->right;
else
while (h->left)
h = h->left;
return (h->n);
}