Created
June 25, 2014 03:34
-
-
Save carlcarl/caf2165f55581746d21c to your computer and use it in GitHub Desktop.
Check is binary search tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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