Home » Data Structure & Algorithms » check if Binary Tree is BST or not

check if Binary Tree is BST or not

 

Question :

WAP to check if a binary tree is  binary search Tree or not .

 

 

Algorithm :

The problem is solved in recursive way.

Hint : Inorder(left->root->right)  traversal of BST gives a ascending sorted array..

Step 1 : If the node is null ,then return false.

Step 2 : Initialize a temp variable to Integer.MIN_VALUE (i.e. -2147483648)

Step 3 : Do a Inorder traversal of tree by travesring left subtree,root and right subtree in order.

Step 4 : While doing Inorder traversal check if the current node’s value  is less than temp.If the current node value is less than temp then it is not binary search tree else assign temp to current node’s value and keep on doing Inorder traversal.

 
 


Leave a comment

Your email address will not be published. Required fields are marked *

eleven − 8 =