Is This a Binary Search Tree?

  • + 0 comments

    Use java 7. I tried below code, it is passing with custom test case with SAME DATA but fails for same data( 5 test failing) in their automation. Not sure why hacker rank guys are not fixing it...

    boolean checkBST(Node root) {
        if (null == root){
            return true;
        }
        //System.out.println("Processing..." + root.data);
        boolean ret = true;
        if (null != root.left){
            if (root.left.data >= root.data){
                ret = false;
                //System.out.println("NOT_OK:Left is bigger or equal:" + root.left.data);
            } else {
                //System.out.println("OK:Left is small:" + root.left.data);
            }
        }
        if (null != root.right){
            if (root.right.data <= root.data){
                ret = false;
                //System.out.println("NOT_OK:Right is smaller or equal:" + root.right.data);
            } else {
                //System.out.println("OK:Right is big:" + root.right.data);
            }
        }
        //if false return here
        if(!ret){
            return false;
        }
    
        return checkBST(root.left) && checkBST(root.right);
    }