Skip to content

Instantly share code, notes, and snippets.

@carlcarl
Created June 25, 2014 03:34
Show Gist options
  • Save carlcarl/caf2165f55581746d21c to your computer and use it in GitHub Desktop.
Save carlcarl/caf2165f55581746d21c to your computer and use it in GitHub Desktop.
Check is binary search tree
// http://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
bool isBST(struct node* root)
{
static struct node *prev = NULL;
// traverse the tree in inorder fashion and keep track of prev node
if (root)
{
if (!isBST(root->left))
return false;
// Allows only distinct valued nodes
if (prev != NULL && root->data <= prev->data)
return false;
prev = root;
return isBST(root->right);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment