[LeetCode]96. Unique Binary Search Trees(DP,卡特兰数)

题目链接:https://leetcode.com/problems/unique-binary-search-trees/#/description

题意:求二叉树种类

卡特兰数,解释一下:n个点取第i个点为父亲,左右儿子可能有C(0)*C(n-1)+C(1)*C(n-2)+...+C(「i/2)*(i/2」)种。这样就可以dp了。

 1 class Solution {
 2 public:
 3     int s[123] = {0,
 4                                 1,
 5                                 2,
 6                                 5,
 7                                 14,
 8                                 42,
 9                                 132,
10                                 429,
11                                 1430,
12                                 4862,
13                                 16796,
14                                 58786,
15                                 208012,
16                                 742900,
17                                 2674440,
18                                 9694845,
19                                 35357670,
20                                 129644790,
21                                 477638700,
22                                 1767263190};
23 
24     int numTrees(int n) {
25         return s[n];
26   }
27 };

 

posted @ 2017-03-14 19:14  Kirai  阅读(140)  评论(0编辑  收藏  举报