码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode 96】96. Unique Binary Search Trees

时间:2016-07-06 23:15:38      阅读:157      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!