-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path110-binary_tree_is_bst.c
More file actions
34 lines (32 loc) · 951 Bytes
/
110-binary_tree_is_bst.c
File metadata and controls
34 lines (32 loc) · 951 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
#include "binary_trees.h"
#include "limits.h"
/**
* 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.
* @lo: The value of a smallest node visited thus far.
* @hi: The value of a 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 lo, int hi)
{
if (tree != NULL)
{
if (tree->n < lo || tree->n > hi)
return (0);
return (is_bst_helper(tree->left, lo, tree->n - 1) &&
is_bst_helper(tree->right, tree->n + 1, hi));
}
return (1);
}
/**
* 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));
}