leetcode96 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.

思路:
需要使用递推关系来解决。
对于n个结点,除去根节点,还剩余n-1个结点。
因此左右子树的结点数分配方式如下所示:
(0,n-1), (1,n-2), (2, n-3), ....(n-1,0)
我们可以简单的得到:
n=0时,种类数为num(n)=1;
n=1时,种类数为num(n)=1;
则可以依次计算得到n个结点时二叉树的种类。
即:
num(n)=num(0)*num(n-1)+num(1)*num(n-2)+num(2)*num(n-3)+...+num(n-1)*num(0)
代码:
实现时引入了HashMap,方便记录。
public int numTrees(int n){
if( n == 0 || n == 1){
return 1;
}
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
hm.put(0, 1);
hm.put(1, 1);
for(int i = 2; i <= n; i++){
int num = 0;
for(int j = 0; j < i; j++){
num += hm.get(j)*hm.get(i-j-1);
}
hm.put(i, num);
}
return hm.get(n);
}

leetcode96 Unique Binary Search Trees的更多相关文章
- LeetCode-96. Unique Binary Search Trees
Description: Given n, how many structurally unique BST's (binary search trees) that store values 1.. ...
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
随机推荐
- Ecplise + Xdebug 一波三折终于能单步调试了
http://my.oschina.net/012345678/blog/152889 Ecplise + Xdebug 一波三折终于能单步调试了 发表于2年前(2013-08-15 15:50) ...
- P1083 借教室
思路:前缀和, c表示对于当前的middle, 前缀和 #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 ...
- jsonObject jsonarray
1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...
- jsmooth 中文乱码
为了一个问题 语言国际国际化 测试了这么多回 ,真佩服自己 jsmooth 中文乱码 语言乱码 的解决办法 : 需要在“JVM” 的参数 中填入一项 : user.language=en 而不是 ...
- MyBatis代码自动生成
MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实 ...
- Hadoop学习笔记(一)
HDFS适合一次写入,多次读取NameNode将文件系统的元数据存储在内存中,因此HDFS所能存储的文件总数受限于NameNode容量类:IOUtil Progressable URL.setURLS ...
- 非Controller类无法使用Service bean解决方案
尝试方案: 1 在Spring的配置文件springmvc.xml中,增加扫描项base-package="zxs.ssm.util",增加你需要使用service的类所在的包 ...
- 借用layer让弹层不限制在iframe内部
使用方法: 1 除了layer的success,end,cancel回掉函数以外其它的layer参数都可以使用. 2 使用前在layer的js后边把该js引入(可以命名为layerExtend). 3 ...
- Popwindow自定义动画(nexus5不支持暂未解决)
遇到一个问题,先记录一下 PopWindow自定义动画 import android.app.Activity; import android.graphics.drawable.BitmapDraw ...
- SublimeText使用技巧
1.安装package control:ctrl+~ 2.调出命令面板:ctrl+shift+p 3.快出查找(goto Anything):ctrl+p 4.修改文件类型:ctrl+shift+p ...