Did you deduce a formula of the tree numbers f(n) or just simulate a tree generation process?

You need to use formula with recursion to calculate the number without exceeding.
Check: http://cs.lmu.edu/~ray/notes/binarytrees/You should not need to simulate entire BST generation.
That will be done for part II.

Simulation is not necessary
Even recursive function can pass, test cases are too weak!
idea:
choose i to be the root, then there i 1 numbers in the left child tree, n  i numbers in the right child tree, calculate child tree recursivelyf(0) = 1
f(1) = 1
f(2) = 2
f(n) = f(n1) * f(1 1) + f(n2) * f(2 1) + f(n3) * f(31) + ... + f(nn) * f(n1)
In addition, lookup table can be used to improve performance!