check if a tree is a binary search tree

TimeToCodeTheRoad picture TimeToCodeTheRoad · Jan 1, 2011 · Viewed 35k times · Source

I have written the following code to check if a tree is a Binary search tree. Please help me check the code:

Okay! The code is edited now. This simple solution was suggested by someone in the posts below:

IsValidBST(root,-infinity,infinity);

bool IsValidBST(BinaryNode node, int MIN, int MAX) 
{
     if(node == null)
         return true;
     if(node.element > MIN 
         && node.element < MAX
         && IsValidBST(node.left,MIN,node.element)
         && IsValidBST(node.right,node.element,MAX))
         return true;
     else 
         return false;
}

Answer

alex picture alex · Jul 30, 2012

right, another simple solution is do an inorder visit

java code here