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) ...
随机推荐
- [ZZ]计算机视觉、机器学习相关领域论文和源代码大集合
原文地址:[ZZ]计算机视觉.机器学习相关领域论文和源代码大集合作者:计算机视觉与模式 注:下面有project网站的大部分都有paper和相应的code.Code一般是C/C++或者Matlab代码 ...
- RSA
https://en.wikipedia.org/wiki/RSA_(cryptosystem)
- JAVA函数的返回值类型详解以及生成随机数的例题
函数的四要素:函数名.输入.输出(返回).加工. 函数分为两种:一种是有返回值得函数,一种是没有返回值的函数. 1. 定义:没有返回值的函数:(当我不需要函数的计算结果再拿出来进行运算的时候,我就不需 ...
- [dpdk] 读开发指南(2)(内容长期整理中)
接续前节. 7 PMD (Poll Mode Driver) A Poll Mode Driver (PMD) consists of APIs, provided through the BSD d ...
- [Virtualization][SDN] VXLAN到底是什么 [转]
写在转发之前: 几个月以前,在北大机房和燕园大厦直接拉了一根光钎.两端彼此为校园内公网IP.为了方便连接彼此机房,我做个一个VPN server在燕园的边界,北大机房使用client拨回.两个物理机房 ...
- PHP正则表达式;数组:for()遍历、 foreach ()遍历、each()list()组合遍历;指针遍历
正则表达式: 1.定界符号 任何字符,一般用 // 2. 模式修正符i 写在定界符外面后面,可不区分大小写 3.preg_replace($reg,&q ...
- C、VDD、VSS、 VEE 和VPP的区别
http://www.cnblogs.com/crazybingo/archive/2010/05/14/1735802.html C.VDD.VSS. VEE 和VPP的区别 在电子电路中,常可以看 ...
- java整合spring和hadoop HDFS
http://blog.csdn.net/kokjuis/article/details/53586406 http://download.csdn.net/detail/kokjuis/970932 ...
- 在sublime中使用less
高亮显示: 可以在Less文件中显示语法高亮,这样看起来会更舒服一些. 按下Ctrl+Shift+P调出命令面板:输入install调出Install Package选项并回车:输入less,选中并安 ...
- java分形树
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; /** * * @ ...