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. 拒绝蛋疼,javascript调试技巧

    chrome的调试技巧网上很多介绍了,这里提两个个人觉得比较有用的: 1.console.log 优点:可以输出json对象,而非字符串,这是 alert() 做不到的 缺点:记得要删除,否则在低版本 ...

  2. java调试打断点和不打断点执行结果不一致问题解决

    java程序在调试的时候需要debug来跟踪一下结果,有一种情况是这样的,正常执行java程序就会出现问题,而断点debug跟踪此方法的时候却是正常的,不断测试结果都是这样,由此判断有可能是因为此方法 ...

  3. Redis(十六):集群搭建(手动和自动)

    一.概述 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2):slave->master ...

  4. Openstack 03 - Nova Compute

    1.前言 非常早之前就開始着手写Openstack 系列的博客了,在写了总体架构和Keystone之后,准备写Nova,可是每次写到一半,自己心里就认为不踏实,由于似乎我并没有真正理解Nova,或者说 ...

  5. union共用体的对齐

    union DATE { char a; ]; double b; }; DATE max; cout<< sizeof(max) << endl; 这个问题很好回答,并且我把 ...

  6. AESDK报错AEGP Magic Error的解决

    原因是要先释放了流,再释放流的值,顺序问题导致报错. 解决方法是先释放流的值,再释放流 mSuites->StreamSuite2()->AEGP_DisposeStreamValue(v ...

  7. C++11中的继承构造函数

    时间:2014.06.19 地点:基地 ------------------------------------------------------------------------- 一.问题描写 ...

  8. android BaseAdapter getView 理解

    ListView是安卓中很经常使用的一个控件. 安卓设计使用Adapter来对ListView进行管理. 可是系统提供的Adapter无法满足一些复杂的显示情况,这个时候我们就须要使用BaseAdap ...

  9. jquery文件导入问题

    开发前台web,导入jquery文件的时候最好用 <script type="text/javascript" src="path"></sc ...

  10. Bootstrap学习笔记(2)--栅格系统深入学习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...