It is easy enough to count the number of calls countTrees that this algorithm uses for a given counter node. After several trial runs, it seems to me that this requires 5 * 3 ^ (n-2) calls for n> = 2, which grows much more slowly than n !. The proof of this statement remains as an exercise for the reader. :-)
The memorable version requires O (n) calls, as you expected.
By the way, the number of binary trees with n nodes is the n-th Catalan number . Obvious approaches to computing C n seem linear in n, so it is probably best to implement a memoized implementation of countTrees .
source share