A binary tree is binary search tree (BST) if :
- If the left subtree nodes value is less than the given node.
- If the right subtree nodes value is greater than the give node
- Both the left and right subtrees are BST.
public class BinaryTreeIsBST {
public boolean IsBST(TreeNode node, int max, int min){
if(node == null)
return true;
if(min < node.value && node.value > max){
return IsBST(node.left,min,node.value) &&
IsBST(node.right,node.value,max);
}
return false;
}
}