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.

看得别人的代码,然后写出来的 :http://www.2cto.com/kf/201312/262420.html

public class UniqueBinarySearchTrees {

    public static void main(String[] args) {
int n = (int)(Math.random()*100); // 随机生成数字
System.out.println(n);
int num = numTrees(n);
System.out.println(num);
} private static int numTrees(int n) {
if (n == 0 || n == 1) {
return n;
}
else {
// 以i为根节点时,其左子树构成为[0,...,i-1],其右子树构成为[i+1,...,n]构成
// 因此,dp[i] = sigma(dp[0...k] * dp[k+1...i]) 0 <= k < i - 1
// dp[3] = dp[0] * dp[2] + dp[1] * dp[1] + dp[2] * dp[0]
int[] num = new int[n+1];
num[0] = num[1] = 1;
for (int i = 2; i < num.length; i++) {
num[i] = 0;
for (int j = 0; j < i; j++) {
num[i] = num[i] + num[j] * num[i-j-1];
}
}
return num[n];
}
} }

Unique Binary Search Trees的更多相关文章

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

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

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

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

  3. 【LeetCode】95. Unique Binary Search Trees II

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

  4. 【leetcode】Unique Binary Search Trees

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

  5. 【leetcode】Unique Binary Search Trees II

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

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

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

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

  8. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  9. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  10. Unique Binary Search Trees I & II

    Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...

随机推荐

  1. ListView遍历每个Item出现NullPointerException的异常(转)

    在使用ListView过程中我们有时候需要遍历取得每个Item项中的一些数据(比如每个Item里面有TextView,需要获取它的文本等等),但是我们在遍历过程中经常会遇到NullPointerExc ...

  2. 在Salesforce中对某一个Object添加 Validation Rule

    在Salesforce中可以对某一个Object添加相应的 Validation Rule 来进行一个全局的条件判断,比如满足什么样的条件的修改允许提交,不满足的要提示相应的错误信息. 要创建一个Va ...

  3. 如何解决""No boot device available(无可用的引导设备)”错误

    首先换一个镜像文件试一试,如果还不行就按以下方法尝试 http://www.parallelsdesktop.cn/xnjxt-wydsb.html Parallels Desktop 常见问题 ht ...

  4. sqlserver 作业调度(作业常用的几个步骤)

    --[作业常用的几个步骤] EXEC msdb.dbo.sp_delete_job EXEC msdb.dbo.sp_add_job EXEC msdb.dbo.sp_add_jobstep EXEC ...

  5. Understanding Kafka Consumer Groups and Consumer Lag

    In this post, we will dive into the consumer side of this application ecosystem, which means looking ...

  6. DSP using Matlab 书内练习Example 2.1

    先上代码,后出结果图. a. n = [-5:5]; x=2*impseq(-2,-5,5) - impseq(4,-5,5); set(gcf,'Color',[1,1,1]) % 改变坐标外围背景 ...

  7. js与jquery异同

    大家都知道jquery是js的一个库,很多东西大多数简写了,让js写起来特别的方便.但是对与学习的人来说,最好是先学会了js再去学jquery会更好.在学得过程中你会发现两者实现的原理是差不多的,但是 ...

  8. jenkins+ant+jmeter搭建持续集成的接口测试平台

    一.jemter接口脚本的编写步骤如下: 1. 编写接口请求 通过录制或者查看接口文档,编写接口请求,进行调试,确保接口调试通过,对于http的请求来说,就是正确的填写域名,查询字符串,查询参数等信息 ...

  9. BZOJ2990 : [Ontak2010]Keyboard

    考虑从$(1,1)$开始搜索移动方案,每次移动坐标的变化量都是$2$. 如果构成了环,那么环的周长肯定是偶数. 考虑这个环一定要被若干个骨牌覆盖,且还有一个位置是空的. 所以得出环的周长是奇数,矛盾, ...

  10. 使用javamail发信过程中的一些问题及解决方法

    http://www.blogjava.net/TrampEagle/archive/2006/05/26/48326.html 今天在研究javamail发信的过程中,出现了一些小问题,现总结如下, ...