首页 > 其他 > 详细

【Leetcode 96】96. Unique Binary Search Trees

时间:2016-07-06 23:14:14      阅读:354      评论:0      收藏:0      [点我收藏+]

This is a BST(binary search tree) numbers counting problem but solved in dynamic programing.

https://discuss.leetcode.com/category/104/unique-binary-search-trees

This solution really gives me a shock. Before solving u need do some mathematical deduce in papers and get the dp function, which fantistically! 

class Solution {
public:
    int numTrees(int n) {
        int G[100];
        for(int i = 0; i <= n; i ++)
            G[i] = 0;
        G[0] = G[1] = 1;
        for(int i = 2; i <= n; i ++){
            for(int j = 1; j <= i; j ++)
                G[i] += G[j - 1] * G[i - j];
        }
        return G[n];
    }
};

 

 

【Leetcode 96】96. Unique Binary Search Trees

原文:http://www.cnblogs.com/luntai/p/5648361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!