A binary search tree (BST) is a node based binary tree data structure which has the following properties.
• The left subtree of a node contains only nodes with keys less than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• Both the left and right subtrees must also be binary search trees.

From the above properties it naturally follows that:
• Each node (item in the tree) has a distinct key.

[ad type=”banner”]

METHOD 1 (Simple but Wrong)
Following is a simple program. For each node, check if left node of it is smaller than the node and right node of it is greater than the node.

[pastacode lang=”c” manual=”int%20isBST(struct%20node*%20node)%20%0A%7B%20%0A%20%20if%20(node%20%3D%3D%20NULL)%20%0A%20%20%20%20return%201%3B%20%0A%20%20%20%20%20%0A%20%20%2F*%20false%20if%20left%20is%20%3E%20than%20node%20*%2F%0A%20%20if%20(node-%3Eleft%20!%3D%20NULL%20%26%26%20node-%3Eleft-%3Edata%20%3E%20node-%3Edata)%20%0A%20%20%20%20return%200%3B%20%0A%20%20%20%20%20%0A%20%20%2F*%20false%20if%20right%20is%20%3C%20than%20node%20*%2F%0A%20%20if%20(node-%3Eright%20!%3D%20NULL%20%26%26%20node-%3Eright-%3Edata%20%3C%20node-%3Edata)%20%0A%20%20%20%20return%200%3B%20%0A%20%20%20%0A%20%20%2F*%20false%20if%2C%20recursively%2C%20the%20left%20or%20right%20is%20not%20a%20BST%20*%2F%0A%20%20if%20(!isBST(node-%3Eleft)%20%7C%7C%20!isBST(node-%3Eright))%20%0A%20%20%20%20return%200%3B%20%0A%20%20%20%20%20%0A%20%20%2F*%20passing%20all%20that%2C%20it’s%20a%20BST%20*%2F%0A%20%20return%201%3B%20%0A%7D” message=”c” highlight=”” provider=”manual”/]

This approach is wrong as this will return true for below binary tree (and below tree is not a BST because 4 is in left subtree of 3)

[ad type=”banner”] METHOD 2 (Correct but not efficient)
For each node, check if max value in left subtree is smaller than the node and min value in right subtree greater than the node.

[pastacode lang=”c” manual=”%2F*%20Returns%20true%20if%20a%20binary%20tree%20is%20a%20binary%20search%20tree%20*%2F%0Aint%20isBST(struct%20node*%20node)%20%0A%7B%20%0A%20%20if%20(node%20%3D%3D%20NULL)%20%0A%20%20%20%20return(true)%3B%20%0A%20%20%20%20%20%0A%20%20%2F*%20false%20if%20the%20max%20of%20the%20left%20is%20%3E%20than%20us%20*%2F%0A%20%20if%20(node-%3Eleft!%3DNULL%20%26%26%20maxValue(node-%3Eleft)%20%3E%20node-%3Edata)%20%0A%20%20%20%20return(false)%3B%20%0A%20%20%20%20%20%0A%20%20%2F*%20false%20if%20the%20min%20of%20the%20right%20is%20%3C%3D%20than%20us%20*%2F%0A%20%20if%20(node-%3Eright!%3DNULL%20%26%26%20minValue(node-%3Eright)%20%3C%20node-%3Edata)%20%0A%20%20%20%20return(false)%3B%20%0A%20%20%20%0A%20%20%2F*%20false%20if%2C%20recursively%2C%20the%20left%20or%20right%20is%20not%20a%20BST%20*%2F%0A%20%20if%20(!isBST(node-%3Eleft)%20%7C%7C%20!isBST(node-%3Eright))%20%0A%20%20%20%20return(false)%3B%20%0A%20%20%20%20%20%0A%20%20%2F*%20passing%20all%20that%2C%20it’s%20a%20BST%20*%2F%0A%20%20return(true)%3B%20%0A%7D%20″ message=”C Programming” highlight=”” provider=”manual”/]

It is assumed that you have helper functions minValue() and maxValue() that return the min or max int value from a non-empty tree

[ad type=”banner”]

METHOD 3 (Correct and Efficient)
Method 2 above runs slowly since it traverses over some parts of the tree many times. A better solution looks at each node only once. The trick is to write a utility helper function isBSTUtil(struct node* node, int min, int max) that traverses down the tree keeping track of the narrowing min and max allowed values as it goes, looking at each node only once. The initial values for min and max should be INT_MIN and INT_MAX — they narrow from there.

/* Returns true if the given tree is a binary search tree 
 (efficient version). */ 
int isBST(struct node* node) 
{ 
  return(isBSTUtil(node, INT_MIN, INT_MAX)); 
} 

/* Returns true if the given tree is a BST and its 
 values are >= min and <= max. */ 
int isBSTUtil(struct node* node, int min, int max) 

Implementation:

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%23include%20%3Climits.h%3E%0A%20%0A%2F*%20A%20binary%20tree%20node%20has%20data%2C%20pointer%20to%20left%20child%0A%20%20%20and%20a%20pointer%20to%20right%20child%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20left%3B%0A%20%20%20%20struct%20node*%20right%3B%0A%7D%3B%0A%20%0Aint%20isBSTUtil(struct%20node*%20node%2C%20int%20min%2C%20int%20max)%3B%0A%20%0A%2F*%20Returns%20true%20if%20the%20given%20tree%20is%20a%20binary%20search%20tree%20%0A%20(efficient%20version).%20*%2F%0Aint%20isBST(struct%20node*%20node)%20%0A%7B%20%0A%20%20return(isBSTUtil(node%2C%20INT_MIN%2C%20INT_MAX))%3B%20%0A%7D%20%0A%20%0A%2F*%20Returns%20true%20if%20the%20given%20tree%20is%20a%20BST%20and%20its%20%0A%20%20%20values%20are%20%3E%3D%20min%20and%20%3C%3D%20max.%20*%2F%0Aint%20isBSTUtil(struct%20node*%20node%2C%20int%20min%2C%20int%20max)%20%0A%7B%20%0A%20%20%2F*%20an%20empty%20tree%20is%20BST%20*%2F%0A%20%20if%20(node%3D%3DNULL)%20%0A%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%0A%20%20%2F*%20false%20if%20this%20node%20violates%20the%20min%2Fmax%20constraint%20*%2F%20%0A%20%20if%20(node-%3Edata%20%3C%20min%20%7C%7C%20node-%3Edata%20%3E%20max)%20%0A%20%20%20%20%20return%200%3B%20%0A%20%0A%20%20%2F*%20otherwise%20check%20the%20subtrees%20recursively%2C%20%0A%20%20%20tightening%20the%20min%20or%20max%20constraint%20*%2F%0A%20%20return%0A%20%20%20%20isBSTUtil(node-%3Eleft%2C%20min%2C%20node-%3Edata-1)%20%26%26%20%20%2F%2F%20Allow%20only%20distinct%20values%0A%20%20%20%20isBSTUtil(node-%3Eright%2C%20node-%3Edata%2B1%2C%20max)%3B%20%20%2F%2F%20Allow%20only%20distinct%20values%0A%7D%20%0A%20%0A%2F*%20Helper%20function%20that%20allocates%20a%20new%20node%20with%20the%0A%20%20%20given%20data%20and%20NULL%20left%20and%20right%20pointers.%20*%2F%0Astruct%20node*%20newNode(int%20data)%0A%7B%0A%20%20struct%20node*%20node%20%3D%20(struct%20node*)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20malloc(sizeof(struct%20node))%3B%0A%20%20node-%3Edata%20%3D%20data%3B%0A%20%20node-%3Eleft%20%3D%20NULL%3B%0A%20%20node-%3Eright%20%3D%20NULL%3B%0A%20%0A%20%20return(node)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%0A%7B%0A%20%20struct%20node%20*root%20%3D%20newNode(4)%3B%0A%20%20root-%3Eleft%20%20%20%20%20%20%20%20%3D%20newNode(2)%3B%0A%20%20root-%3Eright%20%20%20%20%20%20%20%3D%20newNode(5)%3B%0A%20%20root-%3Eleft-%3Eleft%20%20%3D%20newNode(1)%3B%0A%20%20root-%3Eleft-%3Eright%20%3D%20newNode(3)%3B%20%0A%20%0A%20%20if(isBST(root))%0A%20%20%20%20printf(%22Is%20BST%22)%3B%0A%20%20else%0A%20%20%20%20printf(%22Not%20a%20BST%22)%3B%0A%20%20%20%20%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D%20%20″ message=”C Programming” highlight=”” provider=”manual”/]

Time Complexity: O(n)
Auxiliary Space : O(1) if Function Call Stack size is not considered, otherwise O(n)

[ad type=”banner”]

METHOD 4(Using In-Order Traversal)
Thanks to LJW489 for suggesting this method.
1) Do In-Order Traversal of the given tree and store the result in a temp array.
3) Check if the temp array is sorted in ascending order, if it is, then the tree is BST.

Time Complexity: O(n)

We can avoid the use of Auxiliary Array. While doing In-Order traversal, we can keep track of previously visited node. If the value of the currently visited node is less than the previous value, then tree is not BST. Thanks to ygos for this space optimization.

[pastacode lang=”c” manual=”bool%20isBST(struct%20node*%20root)%0A%7B%0A%20%20%20%20static%20struct%20node%20*prev%20%3D%20NULL%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F%2F%20traverse%20the%20tree%20in%20inorder%20fashion%20and%20keep%20track%20of%20prev%20node%0A%20%20%20%20if%20(root)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(!isBST(root-%3Eleft))%0A%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Allows%20only%20distinct%20valued%20nodes%20%0A%20%20%20%20%20%20%20%20if%20(prev%20!%3D%20NULL%20%26%26%20root-%3Edata%20%3C%3D%20prev-%3Edata)%0A%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%20%20%20%20prev%20%3D%20root%3B%0A%20%0A%20%20%20%20%20%20%20%20return%20isBST(root-%3Eright)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20true%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]
 [ad type=”banner”]