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

Tree Dynamic Programming

 
 
如果集合为空,只有一种BST,即空树,
UniqueTrees[0] =1

如果集合仅有一个元素,只有一种BST,即为单个节点
UniqueTrees[1] = 1

 

UniqueTrees[2] = UniqueTrees[0] * UniqueTrees[1]   (1为根的情况)
                  + UniqueTrees[1] * UniqueTrees[0]  (2为根的情况。

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

所以,由此观察,可以得出UniqueTrees的递推公式为
UniqueTrees[i] = ∑ UniqueTrees[0...k] * [i-1-k]     k取值范围 0<= k <=(i-1)

 

 
'''
Created on Nov 13, 2014
 
@author: ScottGu<gu.kai.66@gmail.com, kai.gu@live.com>
'''
class Solution :
    # @return an integer
    def numTrees( self , n):
        uniqueTrees={}
        uniqueTrees[ 0 ]=1
        uniqueTrees[ 1 ]=1
 
        for cnt in range( 2, n+ 1 ):
            uniqueTrees[cnt]= 0
            for k in range( 0, cnt):
                uniqueTrees[cnt]+=uniqueTrees[k]*uniqueTrees[cnt- 1 -k]
 
        return uniqueTrees[n]
       
       
if __name__ == '__main__' :
    sl=Solution()
    print sl.numTrees( 0 ), 0
    print sl.numTrees( 1 ), 1
    print sl.numTrees( 2 ), 2
    print sl.numTrees( 3 ), 3
    print sl.numTrees( 4 ), 4
    print sl.numTrees( 5 ), 5

LEETCODE —— Unique Binary Search Trees [动态规划]的更多相关文章

  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 II 独一无二的二叉搜索树之二

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

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

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

  4. LeetCode: 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 II

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

  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 todo

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

随机推荐

  1. redis随笔集-使用

    redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库 一数据集合: 1.list -- 链表  key-value形式,通过list ID  可以实 ...

  2. Android 6.0 新特性

    首先谈一谈Android 6.0的一些新特性 锁屏下语音搜索 指纹识别 更完整的应用权限管理 Doze电量管理 Now onTap App link 在开发过程中与我们关系最密切的就是"更完 ...

  3. CodeBlocks配置文件位置

    CodeBlock配置混乱,决定重装时,删除程序后,需将配置文件删除. 配置文件位置:C:\Users\Administrator\AppData\Roaming\CodeBlocks

  4. AudioUnit 用法

    1.描述音频单元 AudioComponentDescription desc; desc.componentType = kAudioUnitType_Output; desc.componentS ...

  5. Cairo 下载,测试

    You need to download the all-in-one bundle available here. You can discover this link yourself by vi ...

  6. C#与Java在继承静态类上的区别

    interface ITest { int Get(); } abstract class Test : ITest //此处会出现错误:Programe.Test不实现接口成员Program.ITe ...

  7. php 画图片

    <?php // 使用php操作gd库做图 // 1. 创建一个画布资源 $im = imagecreatetruecolor(80, 40); // 2. 画内容 // 2.1 先位画布准备颜 ...

  8. 深入理解css系列:清除浮动

    如果出现div嵌套,内层元素浮动,而外层包裹的父元素div未设置高度的时候,那么会出现外层不能被撑开的情况. HTML标签代码: <div class="wrap"> ...

  9. DataGridview 自动切换到 下一行

    if (m_dgvList.SelectedRows.Count > 0) { int intCurrApp = m_dgvList.SelectedRows[0].Index; if (int ...

  10. 关于OpenGL的绘制上下文

    什么是绘制上下文(Rendering Context) 初学OpenGL,打开红宝书,会告诉你OpenGL是个状态机,OpenGL采用了客户端-服务器模式,那时觉得好抽象,直到后来了解了绘制上下文才把 ...