Is This a Binary Search Tree?

  • + 5 comments

    Java solution - passes 100% of test cases

    From my HackerRank solutions.

    boolean checkBST(Node root) {
        return checkBST(root, 0, 10000);
    }
    
    boolean checkBST(Node node, int min, int max) {
        if (node == null)
            return true;
        if (node.data < min || node.data > max)
            return false;
        return checkBST(node.left, min, node.data - 1) &&
               checkBST(node.right, node.data + 1, max);
    }
    

    Let me know if you have any questions.