The nth Catalan number is given directly in terms of binomial coefficients by
Cn=n+11(n2n)=(n+1)!n!(2n)!=k=2∏nkn+kfor n≥0
// OJ: https://leetcode.com/problems/unique-binary-search-trees
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
int numTrees(int n) {
long long ans = 1, i;
for (i = 1; i <= n; ++i) ans = ans * (i + n) / i;
return ans / i;
}
};