Unique Binary Search Trees

使用带标记的DP

        int result[1000];
    int numTrees(int n) {
        result[0] = 1;
        result[1] = 1;
        result[2] = 2;
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(result[n] != 0)
            return result[n];
        int num = 0, t;
        for(t = 1; t <= n; t++){
            num = num + numTrees(t-1)*numTrees(n-t);
        }
        return num;
    }

 

posted on 2013-09-22 21:11  waruzhi  阅读(129)  评论(0编辑  收藏  举报

导航