If anyone can tell me where I am going wrong with this method, that would be reaallly awesome, because I have no idea how to make hte method work properly. It consists of two methods actually, a driver method for the recursive method, and the recursive method itself:

public boolean isBST(tNode r) // r is the root of the tree passed in on calling this method
   {
      return isBST(r,r.left.key, r.right.key);
   }

 public boolean isBST(tNode t, int min, int max)
 {
     if(t == null)
     {
         return true;
     }
     if(t.key < min || t.key > max)
     {
         return false;
     }
     boolean leftIs = isBST(t.left,min,t.key);
     if(!leftIs)
     {
         return false;
     }
     return isBST(t.right,t.key+1,max);
 }

Please help. I don't know if my min's and max's are right in the calls, and if they are right in the original driver call.

Code tags added. -Narue

It looks fine, though what happens if you have a one or two node tree here?

return isBST(r,r.left.key, r.right.key);

It would be better to come up with some sentinel min and max value to pass to isBST rather than requiring the tree to have at least three nodes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.