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) ...
随机推荐
- 20145235《Java程序设计》第7周学习总结
教材学习内容总结 13.1 认识时间与日期 格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为椭圆形且速度不一,本身自传减速而造成误差. 世界时(UT):通过观测远方星体跨过子午线而得,受地 ...
- 20145317彭垚 《Java程序设计》第6周学习总结
20145317彭垚 <Java程序设计>第6周学习总结 第十章 输入/输出 10.1 InputStream与OutputStream 1.串流设计的概念 Java将输入/输出抽象化为串 ...
- 【总结】使用jdbc+servlet开发一个bug管理系统的经验总结
开发背景: 公司目前使用Teambition里面的task作为bug管理系统,既没有bug的当前状态,也不能写上bug的详细复现步骤,被assign了任务(该修复bug或者验证bug是否被修复)也没有 ...
- LINQ延迟查询的例子
//linq延迟查询.两次查询结果不同 List<string> l = new List<string>() { "aaa", "bbb&quo ...
- UISearchBar cover first cell of UITableView
1.相当重要的是 tableView.tableHeaderView = searchBar; 这一句一定要在 UIViewController viewDidLoad 的时候执行,否则就会出现 se ...
- Activity初步,初学者必看
Activity是什么? Activity是一个可与用户交互并呈现组件的视图.通俗说就是运行的程序当前的这个显示界面. 如果你还不明白,那么你写过HTML吗,它就好比一个网页.我们程序中的用户视图,都 ...
- Apache的HBase与cdh的hue集成(不建议不同版本之间的集成)
1.修改hue的配置文件hue.ini [hbase] # Use full hostname with security. hbase_clusters=(Cluster|linux-hadoop3 ...
- C++ 实现 发送HTTP Get/Post请求 good
1.简述 最近简单看了一下关于HTTP请求方面的知识,之前一直用Qt来实现,有专门HTTP请求的QNetworkAccessManager类来处理,实现也比较简单,这里主要讲解一下用C++代码来实现H ...
- php--yii2框架错误提示
if($code!=200){ $user=new UserAuth(); $user->mobile=$register['mobile']; $user->password=md5($ ...
- BI 商业智能理解结构图
本文章是在本人实习阶段对BI(商业智能 Business Intelligence)的理解:(如有不足之处请多指教,谢谢) BI 系统负责从多个数据源中搜集数据,并将这些数据进行必要的转换后存储到 ...