Tuesday, November 29, 2011

Check Whether given Tree is a BST or not

  public static boolean isBST(BinaryTree root){
    int min=-9999;
    int max=9999;
    return(isBSTUtil(root,min,max));
   
  }
 
 
  public static boolean isBSTUtil(BinaryTree node, int min, int max){
    if(node==null)
      return true;
    if(node.value < min || node.value > max)
      return false;
    return(isBSTUtil(node.left, min, node.value) && (isBSTUtil(node.right,node.value +1, max)));
  }

No comments:

Post a Comment