Given n, how many structurally unique BST's (binary search trees) that store values 1...n?

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
 
Solution 1:  递归
 class Solution {
public:
int numTrees(int n) {
if(n == )
return ;
else if(n == )
return ;
else
{
int count = ;
for(int i = ; i <= (n-)/; i ++)
{
if(i < n--i)
count += *numTrees(i)*numTrees(n--i);
else
count += numTrees(i)*numTrees(n--i);
}
return count;
}
}
};

Solution 2: dynamic programming . Base case: n==0, n==1时,f(n)==1, f(n)=∑f(i)*f(n-i-1)。即以第i个为根节点,左右子树数目相乘

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

【LeetCode】96 - Unique Binary Search Trees的更多相关文章

  1. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  2. 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...

  3. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  4. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  5. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 【一天一道LeetCode】#95. Unique Binary Search Trees II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. LeetCode OJ 96. Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. [leetcode tree]96. Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. rqnoj-106-最大加权矩形-dp

    和我之前做的那个切西瓜的题目相比就是小巫见大巫了.. 运用最长字段和的原理把O(n^4)转化成O(n^3) #include<stdio.h> #include<string.h&g ...

  2. [原]poj-2488-water-DFS

    题目大意: 输入一个p*q的棋盘, 行用数字表示, 列用大写字母表示 , 1 <= p*q <= 26, 输出能够把棋盘全部空格走完且每个空格只走一次的字典序最小的路径.不存在则输出“im ...

  3. sgen.exe" exited with code 1.解决方法

    VS中有时候编译项目会出现这样的错误: 错误   "sgen.exe" exited with code 1.即(错误   “sgen.exe”已退出,代码为 1.) 解决办法: ...

  4. sql, plsql 总结

    /* *====================================== basic sql ========================================== */ - ...

  5. JS function的定义方法,及function对象的理解。

    废话篇: 今天看到了Function的内容,各种晕,各种混淆有木有.简直是挑战个人脑经急转弯的极限啊.不过,最终这一难题还是被我攻克了,哇咔咔.现在就把这东西记下来,免得到时候又忘了就悲催了.... ...

  6. hdu1050(贪心)

    囧 . 想了好久,一开始想的是一个连通图怎样用黑白两色染色,想了各种算法发现都不好做,然后灵机一动这不是网路流吗,然后想怎么建图,如果转换成网络流这题就好做了,建图加个二分应该就可以解决了,最后又发现 ...

  7. 一招解决OpenERP8.0安装旧版模块报错

    有喜欢尝鲜的网友开始玩8.0了,可是版本还没发布,社区的很多特别好的模块还没有升级到8,所以经常碰到模块无法安装的问题. No module name osv 网友提出将模块的 from osv im ...

  8. BZOJ 1911 特别行动队

    另一个版本的斜率优化...这个要好理解一些. #include<iostream> #include<cstdio> #include<cstring> #incl ...

  9. 【英语】Bingo口语笔记(19) - 如何用英语叙旧

  10. unity, 在保持场景根节点Transform不变且Hierarchy结构不变的前提下整体旋转场景

    比如我们摆出下面结构: 其Hierarchy如下: 其中根节点road的Transform是如下干净的原始状态: 现在想保持road的Hierarchy和Transform都不变的情况下将road旋转 ...