Could someone please verify the validity of my algorithm? I am assuming that a non-null value is initially passed into the function:
boolean isBinarySearchTree(Node *root){
if(!root)
return true;
if(root.left.data<root.data && root.right.data>root.data){
isBinarySearchTree(root.left);
isBinarySearchTree(root.right);
}
else
return false;
}
EDIT:
boolean driver(Node *root){
return doesFollowBSTRulesLeft(root, root.value) && doesFollowBSTRulesRight(root, root.value);
driver(root.left);
driver(root.right);
}
boolean doesFollowBSTRulesLeft(Node *root, int originalRootValue){
if(root.value >= originalRootValue)
return false;
doesFollowBSTRulesLeft(root.left, originalRootValue);
doesFollowBSTRulesLeft(root.right, originalRootValue);
return true;
}
boolean doesFollowBSTRulesRight(Node *root, int originalRootValue){
if(root.value <= originalRootValue)
return false;
doesFollowBSTRulesRight(root.left, originalRootValue);
doesFollowBSTRulesRight(root.right, originalRootValue);
return true;
}