Unique Binary Search Trees

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

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 ;
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;
}
}
};

解法二:动态规划

class Solution {
public:
int numTrees(int n) {
if(n== || n == )
return ; vector<int> v(n+, );
v[] = ;//n==0
v[] = ;//n==1
for(int i = ; i <= n; 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 (2 solutions)的更多相关文章

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

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

  2. 【LeetCode】96 - Unique Binary Search Trees

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

  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】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. zoj 3621 Factorial Problem in Base K 数论 s!后的0个数

    Factorial Problem in Base K Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onli ...

  2. JSP myecplise项目移植到ecplise

    把myecplise项目移植到ecplise的一些细节: 参考于http://www.cnblogs.com/liushuijinger/p/3396063.html 因为个人需要,需要把JSP项目从 ...

  3. MySQL之分页问题解决

    最近遇到很多大分页的问题,服务器负载很高,SQL的执行时间也较长,非常的头痛.下面我们就说说分页问题的解决. 大家都知道对于mysql来说,select * from table where x=‘? ...

  4. 【JSP EL】EL表达式里日期按照格式显示

    转:http://blog.csdn.net/kaishuaige/article/details/8505174 JSP页面用EL表达式 输出date格式     1.头上引入标签 <%@ t ...

  5. 体验NW.js打包一个桌面应用

    1.安装nw,(也可在官网下载然后配置变量) npm install nw -g 一个最最简单的nw应用,只需要有index.html和package.json文件即可 2.项目准备,目录结构 app ...

  6. Android之MVC模式

    MVC (Model-View-Controller):M是指逻辑模型,V是指视图模型,C则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计数据 你可以分别用柱状图.饼图来表示.一种视图模型也 ...

  7. 数学图形(1.28) EPI线

    貌似由双曲线组成的图形.有时会像个自行车的轮子. 相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 #http://www.m ...

  8. django from表单基础知识点

    今日概要: - 内容拾遗 - 分页 - Form验证 1.内容拾遗 - 新URL -提交时,保留之前的内容? - 对话框 - var data = $('#fmForm表单的ID').serializ ...

  9. 修改url地址参数

    使用changeURLPar('http://www.baidu.com?page=2&bb=cc','page',10) 得到结果http://www.baidu.com?page=10&a ...

  10. C++对C的改进(2)

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-change2.html,转载请注明源地址 区别一:原型声明的区别 原型声明的概念: 函数要先定义再使用,如果 ...