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. c、c++知识点

    一. (1)在linux下类似uint8_t这样的文件定义在头文件<stdint.h>里面   (2)截取了stdint.h头文件里的一些常用部分 二.c++中c_str()用法 函数返回 ...

  2. hadoop的thriftserver配置

    说明:hadoop版本:hadoop-1.2.1.tar.gz.linux系统12.04,不过这里跟系统无关,可能安装软件的命令有差别. 一.概述 默认的hbase安装包已经有了thrift服务,使用 ...

  3. 【Cocos2d-X游戏实战开发】捕鱼达人之游戏场景的创建(六)

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为cocos2d-x-2.1.5)    博主发现前两个系列的学习教程被严重抄袭,在这里呼吁大家请尊重开发者的劳动成果, 转载的时候请务必注 ...

  4. deciaml(十进制浮点运算)

    # -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...

  5. Unity目录结构

    http://www.cnblogs.com/liudq/p/5540051.htmlUnity中有几个默认目录 Unity5.x Resources 项目中默认的资源路径,会直接打包到游戏包中.即使 ...

  6. BOOST 线程完全攻略 - 结束语

    modulethread扩展多线程破解通讯 全文介绍了3个boost::thread的扩展类,希望能给大家书写多线程代码带来便捷. thread -> controlled_module_ex ...

  7. UIButton-初识IOS

    今天,我学到了所有app经常用到的UIButton控件,废话不多说,这些都是我学习的时候总结的一些,希望可以帮到以后的初学者,IOS初学不应该直接拖拽,感觉不易于理解,所以我总结的基本上全是纯代码编辑 ...

  8. Java中遍历Map对象的方法

    方法一: 在for-each循环中使用entries来遍历 这是最常见的遍历方式,在需要获取key和value时使用. Map<Integer, Integer> map = new Ha ...

  9. web去掉浏览器自带默认样式

    @charset "utf-8"; ;;} body{font-size:12px;} img{border:none;} ul,ol{list-style:none;} inpu ...

  10. 模板页 相对路径 JS 加载问题

    问题:我在master页面中引入了如下js文件:<script type="text/javascript" src="http://www.cnblogs.com ...