题目描述:

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. POJ 3678

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7391   Accepted: 2717 Descr ...

  2. 安装DirectX SDK (June 2010) 失败(Error Code S1023)(转)

    解决: 在控制面板里面搜索“Microsoft Visual C++*”,然后卸载,这个工具的名字大概是: Microsoft Visual C++ 2010 x86/x64 redistribuab ...

  3. Java7编程高手进阶读书笔记--final学习

    这段时间终于有了一些自己的时间,在网上淘了一本书把java学习下,顺便记下每日的学习心得 工作快两年多了,才知道基础的东西永远看的时候都有一个新的体验,今天中午看了下final,把自己炒的代码贴在这以 ...

  4. ruby 学习 -- Array --2

    定义: [1, 2, 3] # An array that holds three Fixnum objects [-10...0, 0..10,] # An array of two ranges; ...

  5. java web线程池

    线程池 要知道在计算机中任何资源的创建,包括线程,都需要消耗系统资源的.在WEB服务中,对于web服 务器的响应速度必须要尽可能的快,这就容不得每次在用户提交请求按钮后,再创建线程提供服务 .为了减少 ...

  6. 1.BOM学习

    1.bom.html <html> <head> <title>bom演示</title> <script type="text/jav ...

  7. 关于java中split的使用

    之前在http://shukuiyan.iteye.com/blog/507915文中已经叙述过这个问题,但是最近一次笔试中居然有碰到了这个知识点,而且还做错了,囧!学艺不精啊.题目大概是这样的: ) ...

  8. QT中显示GIF图片

    在QT中要显示GIF图片,不能通过单单的添加部件来完成. 还需要手动的编写程序. 工具:QT Creator 新建一个工程,我们先在designer中,添加一个QLabel部件. 如下图: 将QLab ...

  9. 世界上还有一个东西叫Virtual Pascal

    官网是:http://web.archive.org/web/20060312064321/http://www.vpascal.com/news.php?item.16 不过2005年就不再维护了. ...

  10. C#中检测某个类(方法、程序集等各种部分)是否应用了指定的特性以及对特性的一些简单操作

    前言:不管是自定义的一些特性,或者是C#中内置的特性,均继承自Attribute这个类,这个类也提供了一些方法,方便我们使用. Attribute类有三个静态方法:1.IsDefined,如果有指定的 ...