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                1                      2                       3             3
    \                 \                 /      \                  /              / 
      3               2              1       3               2             1
    /                   \                                       /                  \
 2                       3                                   1                    2

比如,以1为根的树有几个,完全取决于有二个元素的子树有几种。同理,2为根的子树取决于一个元素的子树有几个。以3为根的情况,则与1相同。

定义Count[i] 为以[0,i]能产生的Unique Binary Tree的数目,

如果数组为空,毫无疑问,只有一种BST,即空树,
Count[0] =1

如果数组仅有一个元素{1},只有一种BST,单个节点
Count[1] = 1

如果数组有两个元素{1,2}, 那么有如下两种可能
1                       2
  \                    /
    2                1
Count[2] = Count[0] * Count[1]   (1为根的情况)
                  + Count[1] * Count[0]  (2为根的情况。

再看一遍三个元素的数组,可以发现BST的取值方式如下:
Count[3] = Count[0]*Count[2]  (1为根的情况)
               + Count[1]*Count[1]  (2为根的情况)
               + Count[2]*Count[0]  (3为根的情况)

所以,由此观察,可以得出Count的递推公式为
Count[i] = ∑ Count[k] * [i-k-1]     0<=k<i-1
问题至此划归为一维动态规划。

 public int numTrees(int n) {
// Start typing your Java solution below
// DO NOT write main() function
int[] count = new int[n+1];
count[0] = 1;
count[1] = 1;
for(int i = 2; i <= n; i++){
for(int j = 1; j <=i; j++){
count[i] += count[j - 1] * count[i - j];
}
}
return count[n];
}

[Note]
这是很有意思的一个题。刚拿到这题的时候,完全不知道从那下手,因为对于BST是否Unique,很难判断。最后引入了一个条件以后,立即就清晰了,即
当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:
以i为根节点的树,其左子树由[0, i-1]构成, 其右子树由[i+1, n]构成。

ref:http://fisherlei.blogspot.com/2013/03/leetcode-unique-binary-search-trees.html

leetcode -- Unique Binary Search Trees todo的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  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 - Unique Binary Search Trees II

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

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

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

  6. Leetcode: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 @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...

  8. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  9. Leetcode Unique Binary Search Trees

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

随机推荐

  1. js处理url

    需求:用js获得url的电话号码和状态 针对url地址:http://www.deikang.com/index.php?tel=15811296111&status=1&id=100 ...

  2. Java中Math类的几个四舍五入方法的区别

    JAVA取整以及四舍五入 下面来介绍将小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则:Math.ceil()执 ...

  3. 常用RGB颜色表

      作者:张家珩2005-12-02 20:51分类:默认分类     R G B 值   R G B 值   R G B 值 黑色 0 0 0 #000000 黄色 255 255 0 #FFFF0 ...

  4. Node Redis 小试

    Redis 是一个高性能的 key-value 数据库,为了保证效率,数据都是缓存在内存中,在执行频繁而又复杂的数据库查询条件时,可以使用 Redis 缓存一份查询结果,以提升应用性能. 背景 如果一 ...

  5. WebSocket的几个模块(node.js)(未完)

    1.ws模块 npm install ws 2.nodejs-websocket npm nodejs-websocket 3.socket.io模块 npm install socket.io

  6. php实现aes加密类

    php实现的aes加密类,代码中有使用方法. <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null ...

  7. 【原创】k8s源代码分析-----kubelet(1)主要流程

    本人空间链接http://user.qzone.qq.com/29185807/blog/1460015727 源代码为k8s v1.1.1稳定版本号 kubelet代码比較复杂.主要是由于其担负的任 ...

  8. nginx在用户使用ie的使用重定向到/nginx-MSIE目录下

    [root@web01 ]# cat /app/server/nginx/conf/rewrite/default.conf #rewrite ^/index\.html /index.php las ...

  9. Nokia Imaging SDK滤镜使用入门

    简要说明: 通过对示例图片分别运用 LomoFilter和SketchFilter两个滤镜(Nokia Imaging SDK 内置), 来学习滤镜的基本使用和参数的设置.本工程的代码量比较少,也很容 ...

  10. python中sorted方法和列表的sort方法使用详解

    一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...