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. 在windows系统的文件右键菜单中增加“命令提示符”

    本实用小工具能够在windows系统的文件右键菜单中增加“命令提示符”,方便快速进入制定文件的命令提示窗口,避免逐层输入或复制文件夹路径,极其实用. 工具下载地址如下:360云盘(访问密码:5b71) ...

  2. 【MySQL 安装过程2】MySQL安装到 最后一部 未响应 的解决方案

    首先我们应该做的 是在控制面板将MySQL 卸载.再进行以下的操作: 1.在开始菜单下,点击运行,输入regedit,进入注册表编辑器目录下 2.在注册表编辑器里system下找到controlset ...

  3. X-UA-Compatible属性的解释

    问题描述: 代码如下: <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE& ...

  4. JAVA 加减乘除

    package homework; import javax.swing.JOptionPane; public class suanshu { public static void main(Str ...

  5. 《DSP using MATLAB》示例Example4.15

    代码: b = [1/3, 1/3, 1/3]; a = [1, -0.95, 0.9025]; % x(n) y(n) coefficient [R, p, C] = residuez(b,a) M ...

  6. 03 Javascript初识

    Javascript语言(★★★★★)      Javascript是基于对象和事件驱动的脚本语言,作用在客户端.              特点: * 交互性 * 安全性(不能访问本地的硬盘)   ...

  7. Lost Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9669   Accepted: 6228 Description N (2 ...

  8. linux tomcat配置https

    自己生成一个ssl证书,因为是自己生成的所以该证书,不被浏览器信任(具体表现https前面有个X) [root@centos apache-tomcat-]# keytool -genkey -ali ...

  9. three.js加入监控

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. POJ3686 The Windy's(最小费用最大流)

    题目大概说要用m个工厂生产n个玩具,第i个玩具在第j个工厂生产要Zij的时间,一个工厂同一时间只能生成一个玩具,问最少的用时. 这题建的图不是很直观.. 源点向玩具连容量1费用0的边 将每个工厂拆成n ...