[leetcode]Unique Binary Search Trees

catalan number

不过不懂怎么推导TAT

 

用递推来实现吧

 

class Solution {
public:
    int numTrees(int n) {
        vector<int> f(n+1 , 0);
        f[0] = 1;
        f[1] = 1;
        for(int i = 2 ; i <= n ;  ++i) {
            for(int k = 1 ; k <= i ; ++k) {
                f[i] += f[k-1] * f[i-k];
            }
        }
        return f[n];
    }
};

 

你可能感兴趣的:(Binary search)