Determine if a binary tree is a binary search tree (BST)

Write a function isBST(Node *head) to verify if a given binary tree is a Binary Search Tree (BST) or not.

C++:
bool isBST(Node* head, int min, int max)
{
   if(head==NULL)
       return true;
   if((head->value<min)||(head->value>max))
       return false;
   return ((isBST(head->left,min,head->value))
       &&(isBST(head->right,head->value,max)));
}
Advertisements
This entry was posted in Binary search, Tree. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s