def find_val_or_next_smaller(bst, x):
    """Get the greatest value <= x in a binary search tree.
    Returns None if no such value can be found.
    """
    if bst is None:
        return None
    elif bst.val == x:
        return x
    elif bst.val > x:
        return find_val_or_next_smaller(bst.left, x)
    else:
        right_best = find_val_or_next_smaller(bst.right, x)
        if right_best is None:
            return bst.val
        return right_best