题目:

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的更多相关文章

  1. LeetCode-96. Unique Binary Search Trees

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

  2. Leetcode96.Unique Binary Search Trees不同的二叉搜索树

    给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...

  3. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

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

  4. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  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】Unique Binary Search Trees

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

  7. 【leetcode】Unique Binary Search Trees II

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

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

  9. LeetCode: Unique Binary Search Trees II 解题报告

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

随机推荐

  1. vFloppy1.5-虚拟启动软盘

    vFloppy1.5为纯绿色免费软件,安装后不需要引导盘即可进入DOS,支持图形化访问NFTS系统格式,还可添加由光盘启动菜单选项,对于没有光驱,软驱的朋友非常实用. 到BIOS设置由光盘启动,或者打 ...

  2. 原创:CentOS6.4配置solr 4.7.2+IK分词器

    本文原创,转载请注明出处 相关资源下载:http://pan.baidu.com/s/1pJPpiqv 1.首先说明一下 solr是java语言开发的企业级应用服务器,所以你首先安装好jdk,配置好j ...

  3. Java中的匿名对象

    匿名对象就是没有明确给出名字的对象.一般匿名对象只使用一次,而且匿名对象只在堆内存中开辟空间,而不存在栈内存的引用. 一个普通的常量字符串就可以表示一个匿名String对象. 比如可以 int len ...

  4. php数据缓存

    用php进行微信开发时,碰到access_token长久保存的问题,以前都是用框架里的Cache直接set.get一下就完了.现在没框架可用了,只好自己动手写一个cache暂时用. 这个Cache类用 ...

  5. mark down pad2

    邮箱:Soar360@live.com授权秘钥:GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2M5SN6bnxn ...

  6. js判断url是否有效

    方法一:(仅适用于ie) function CheckStatus(url) { XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP") ...

  7. html代码转义到js时,往往会遇到问题,这代码实现html和js互转

    这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  8. 兼容IE的CSS的”引入方式“

    1.给IE浏览器的7版本来提供需要引用的样式(如果把7去掉则给所有的IE浏览器提供样式) <!--[if IE 7]> <Link type="text/css" ...

  9. HTML文件基本结构

    固定结构: <html> <head>...</head> <body>...</body> </html>1,<html ...

  10. Nagios Looking Glass 本地文件包含漏洞

    漏洞名称: Nagios Looking Glass 本地文件包含漏洞 CNNVD编号: CNNVD-201310-682 发布时间: 2013-10-31 更新时间: 2013-10-31 危害等级 ...