-
Notifications
You must be signed in to change notification settings - Fork 0
/
110-binary_tree_is_bst.c
executable file
·40 lines (36 loc) · 1.05 KB
/
110-binary_tree_is_bst.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
#include "binary_trees.h"
int binary_tree_is_bst(const binary_tree_t *tree);
int is_bst_helper(const binary_tree_t *tree, int low, int high);
/**
* binary_tree_is_bst - Checks if a binary tree is a valid Binary Search Tree.
*
* @tree: A pointer to the root node of the tree to check.
*
* Return: 1 if tree is a valid BST, and 0 otherwise.
*/
int binary_tree_is_bst(const binary_tree_t *tree)
{
if (tree == NULL)
return (0);
return (is_bst_helper(tree, INT_MIN, INT_MAX));
}
/**
* is_bst_helper - Checks if a binary tree is a valid binary search tree.
*
* @tree: A pointer to the root node of the tree to check.
* @low: The value of the smallest node visited thus far.
* @high: The value of the largest node visited this far.
*
* Return: If the tree is a valid BST, 1, otherwise, 0.
*/
int is_bst_helper(const binary_tree_t *tree, int low, int high)
{
if (tree != NULL)
{
if (tree->n < low || tree->n > high)
return (0);
return (is_bst_helper(tree->left, low, tree->n - 1) &&
is_bst_helper(tree->right, tree->n + 1, high));
}
return (1);
}