Question

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

Solution

Key to the problem here is to separate this problem into two smaller problems, unique BST's number of right child tree and left child tree.

The pseudocode is like:

for (int i = 1; i <= n; i++) {

  result = result + nums[1 ~ i - 1] + nums[i + 1 ~ n]

}

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

Unique Binary Search Trees 解答的更多相关文章

  1. Unique Binary Search Trees II 解答

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

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

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

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

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

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

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

  5. 【leetcode】Unique Binary Search Trees

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

  6. 【leetcode】Unique Binary Search Trees II

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

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

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

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

  9. LeetCode - Unique Binary Search Trees II

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

随机推荐

  1. windows media player 中播放pls的方法

    windows media player目前只能播放 wpl 和 asm格式的列表文件.而linux下mplayer和vlc支持的pls,很遗憾没法支持. 不过,老外写了个“open pls in w ...

  2. dispatch_group_async

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dispat ...

  3. IOS基于新浪微博开放平台微博APP

    1.基于新浪微博开放平台APP源码 2.gitHub源代码下载地址 https://github.com/whzhaochao/SinaWeiBoOpen 3.用到的第三放开源库 3.1  RTLab ...

  4. MFC获取当前时间

    获取按钮消息响应函数: void CTest17GetTimeDlg::OnGetTime() { // TODO: 在此添加控件通知处理程序代码 //UpdateData(true); CTime  ...

  5. ThinkPHP整合百度Ueditor图文教程

    ThinkPHP整合百度Ueditor图文教程 ThinkPHP整合百度Ueditor,基于黄永成老师的视频说明的申明:最好大家都能写绝对路径的都写好绝对路径比如:window.UEDITOR_HOM ...

  6. css解决文字垂直居中

    参考链接: http://www.cnblogs.com/lufy/archive/2012/09/12/2681972.html   http://zhidao.baidu.com/question ...

  7. Android SQLite的使用2(非原创)

    1.数据库的增.删.改.查:execSQL方法 public void insertAction() {//添加信息 db.execSQL("insert into Emp(name,sal ...

  8. SQL server根据值搜表名和字段

    DECLARE @what varchar(800) SET @what='lll' --要搜索的字符串 DECLARE @sql varchar(8000) DECLARE TableCursor ...

  9. SQL Server 数据类型 Decimal介绍

    为SQL Server 数据类型,属于浮点数类型.存储数据范围是: -1038~1038-1 的固定精度和小数位的数字.一个decimal类型的数据占用了2~17个字节.decimal数据类型在SQL ...

  10. 图的广度、深度优先遍历 C语言

    以下是老师作为数据结构课的作业的要求,没有什么实际用处和可以探讨和总结的的地方,所以简单代码直接展示. 宽度优先遍历: #include<cstdio> #include<iostr ...