Is This a Binary Search Tree?

  • + 0 comments

    My Java 8 Solution

    boolean checkBST(Node root) {
            return isBST(root, null, null);
        }
    
        boolean isBST(Node root, Integer min, Integer max) {
            if (root == null) {
                return true;
            }
            
            if ((min != null && root.data <= min) || (max != null && root.data >= max)) {
                return false;
            }
            
            return isBST(root.left, min, root.data) && isBST(root.right, root.data, max);
        }