leetcode Unique Binary Search Trees

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

 

posted @ 2013-09-04 14:37  代码改变未来  阅读(125)  评论(0编辑  收藏  举报