题目:

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

思路:

找规律

1. 没有节点,BST (Binary Search Tree)为1个, 即空树

2. 有一个节点,BST为1个,即自身为根节点

3. 有二个节点,BST为2个,1为root,左为空,右为2;2为root,左为1,右为空,其实就是dp[0]*dp[1] + dp[1]*dp[0]

1                  2

\               /

2            1

4. 有三个节点

 1               1                  2                       3             3
  \                \               /    \                  /              /
  3               2              1       3             2             1
 /                   \                                  /                 \
2                      3                               1                    2

可以推出dp[3] = dp[0]*dp[2] + dp[1]*dp[1] + dp[2]*dp[0]

package bst;

public class UniqueBinarySearchTrees {

    public int numTrees(int n) {
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
dp[i] += dp[j] * dp[i - 1 - j];
}
}
return dp[n];
} public static void main(String[] args) {
// TODO Auto-generated method stub
UniqueBinarySearchTrees u = new UniqueBinarySearchTrees();
System.out.println(u.numTrees(3));
} }

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

随机推荐

  1. SQL 游标使用实例

    IF EXISTS(SELECT *FROM sysobjects WHERE name='sp_ContestSubmit') DROP PROC sp_ContestSubmit GO -- == ...

  2. nodejs express 框架解密1-总体结构

    本文是基于express3.4.6的. 1.express 代码结构为: bin/express 是在命令行下的生成express 框架目录文件用的 lib/express 是框架的入口文件 lib/ ...

  3. callable object与新增的function相关 C++11中万能的可调用类型声明std::function<...>

    在c++11中,一个callable object(可调用对象)可以是函数指针.lambda表达式.重载()的某类对象.bind包裹的某对象等等,有时需要统一管理一些这几类对象,新增的function ...

  4. jQuery 通配符

    通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code']");//id属 ...

  5. SAP-GR/IR的理解

    SAP-GR/IR的理解   http://shousitukyou.blog.163.com/blog/static/13868005820109127046318/ GR/IR 1,采购的an i ...

  6. C#集合--数组

    Array类是所有一维和多维数组的隐式基类,同时也是实现标准集合接口的最基本的类型.Array类实现了类型统一,因此它为所有数组提供了一组通用的方法,不论这些数组元素的类型,这些通用的方法均适用. 正 ...

  7. Moto G 通话没声音

    入手了摩托罗拉被 Google 收购后推出的第二款手机 Moto G (第一款是 Moto X) 后发现有个问题,有时候会莫名其妙地通话没声音,你听不到对方的,对方也听不到你的,从网上的搜索结果来看, ...

  8. C#下水晶报表打印自定义纸张

    在VB6中,如果要自定义纸张很方便:  Rpt.PaperSize = crPaperUser  Rpt.SetUserPaperSize lZZG, lZZK 但在C#中却不行了,没有发现 SetU ...

  9. android 模拟器

    参考:http://www.syscs.com/node/504 --skin WVGA800 - -no-boot-anim -wipe-: the dpi for the device you a ...

  10. 使用NSKeyedArchiver归档

    将各种类型的对象存储到文件中,而不仅仅是字符串.数组和字典类型,有一种更灵活的方法.就是利用NSKeyedAarchiver类创建带键(keyed)的档案来完成. Mac OS X从版本10.2开始支 ...