Skip to content

Instantly share code, notes, and snippets.

@reyou
Created May 24, 2018 01:51
Show Gist options
  • Save reyou/bc06db282748b5e252fb58d31ac6b825 to your computer and use it in GitHub Desktop.
Save reyou/bc06db282748b5e252fb58d31ac6b825 to your computer and use it in GitHub Desktop.
struct Node
{
int data;
Node *left;
Node *right;
}
bool IsSubtreeLesser(Node *root, int value)
{
if(root == NULL) return true;
if(root->data <= value && IsSubtreeLesser(root->left, value) && IsSubtreeLesser(root->right, value))
{
return true;
}
else {
return false;
}
}
bool IsSubtreeGreater(Node *root, int value)
{
if(root == NULL) return true;
if(root->data <= value && IsSubtreeGreater(root->left, value) && IsSubtreeGreater(root->right, value))
{
return true;
}
else {
return false;
}
}
bool IsBinarySearchTree(Node *root)
{
if(root == NULL) return true;
if (IsSubtreeLesser(root->left, root->data) && IsSubtreeGreater(root->right, root->data)
&& IsBinarySearchTree(root->left) && IsBinarySearchTree(root->right))
{
return true;
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment