题目描述:

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

解题思路:

动态规划法。

用G(n)表示长度为n组成的二叉搜索树的数目;

G(0) = 1, G(1) = 1

F(i,n)表示以i为根节点,长度为n组成的二叉搜索树的数目。

从而G(n) = F(1,n) + F(2,n) + ...+ F(n,n)

而F(i,n) = G(i - 1) * G(n - i)  1<=i <=n

从而G(n) = G(0) * G(n - 1) + G(1) * G(n - 2) + ... + G(n - 1) * G(0)

代码如下:

public class Solution{
public int numTrees(int n){
int[] res = new int[n + 1];
res[0] = res[1] = 1; for(int i = 2; i <= n; i++){
for(int j = 0; j <= i - 1; j++){
res[i] += res[j] * res[i - 1 - j];
}
} return res[n];
}
}

  

Java [Leetcode 96]Unique Binary Search Trees的更多相关文章

  1. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  2. 52. leetcode 96. Unique Binary Search Trees

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

  3. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  4. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

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

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

  6. leetcode 96 Unique Binary Search Trees ----- java

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

  7. [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数

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

  8. [leetcode] 96 Unique Binary Search Trees (Medium)

    原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...

  9. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

随机推荐

  1. javascript的Function 和其 Arguments

    http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...

  2. django转义safe

    “何谓转义?就是把html语言的关键字过滤掉.例如,<div>就是html的关键字,如果要在html页面上呈现<div>,其源代码就必须是<div> 默认情况下,d ...

  3. java集合TreeMap应用---求一个字符串中,每一个字母出现的次数

    package cn.itcast.p1.map.test; import java.util.Iterator; import java.util.Map; import java.util.Tre ...

  4. 2014多校第七场1003 || HDU 4937 Lucky Number

    题目链接 题意 : 给定一个十进制n,让你转化成某个进制的数,让这个数只包含3 4 5 6这些数字,这个进制就成为n的幸运数字,输出有多少幸运数字,例如19,5进制表示是34,所以5是19的一个幸运数 ...

  5. JS初学的一些易错的知识点

    1.  false ,0 , "" ,undefined , null  在Boolean 环境下当成 false: null  在数值环境下当成 0: undefined 在数值 ...

  6. mybatis 打印日志log4j.properties

    log4j.rootLogger=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log ...

  7. ubuntu common

    系统信息 # uname -a              # 查看内核/操作系统/CPU信息 # cat /etc/issue        # 查看操作系统版本 #cat /proc/version ...

  8. CentOS增加硬盘

    1.查看新硬盘     #fdisk –l      新添加的硬盘的编号为/dev/sdb 2.硬盘分区     1)进入fdisk模式     #/sbin/fdisk /dev/sdb     2 ...

  9. SSIS ->> Event Handler

    Event Handler支持在某个事件触发的时候定义好处理该事件的逻辑,比如错误事件触发是该怎么处理.它跟Control Flow界面相似,就好像执行了另外一个包一样.Event Handler不仅 ...

  10. 利用SOLR搭建企业搜索平台 之——模式配置Schema.xml

    来源:http://blog.csdn.net/awj3584/article/details/16963525 schema.xml这个配置文件可以在你下载solr包的安装解压目录的\solr\ex ...