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. UliPad ----python 开发利器

    安装wxPython                                                                                          ...

  2. java第一天学习作业及答案

    作业一 一.选择题 1.选出在java中有效的注释声明(AD)(选两项) A.//这是注释 B.*/这是注释*/ C./这是注释 D./*这是注释*/ 2.在控制台运行一个java程序,使用的命名正确 ...

  3. Python开发入门与实战14-基于Extjs的界面

    14. 基于Extjs的界面 上一章我们实现了一个原生的html例子,本章我们将采用Extjs实现界面的展现,来说明MVC模式下我们是怎么考虑界面与业务层的关系的. 14.1. 引用Extjs目录 首 ...

  4. 持续集成配置-Teamcity

    1.安装目录\buildagent\work\每个项目下一个文件夹\src下是源 目的: 2. 安装目录\buildagent\work\每个项目下一个文件夹\src下是源 目的是teamcity中配 ...

  5. linux-yum

    yum - Yellowdog Updater Modified 简介: Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中 ...

  6. 打开现有的pdf,并插入一个图片

    不说了,直接代码 T_ScanUploadData file = _IScanUploadDataAccessService.GetScanUploadData(id); byte[] filedat ...

  7. C#导入Excel遇到数字字母混合列数据丢失解决

    错误重现: ----------------------------------------------------------------------- 在导入Excel读取数据时,其中的一个字段保 ...

  8. JS生成二维码,允许中文转码

    一.使用jquery-qrcode生成二维码 先简单说一下jquery-qrcode,这个开源的三方库(可以从https://github.com/jeromeetienne/jquery-qrcod ...

  9. UIKit框架之UIGestureRecognizer

    ---恢复内容开始--- 1.继承链:NSObject 2.UIGestureRecognizer的子类有以下: UITapGestureRecognizer :点击 UIPinchGestureRe ...

  10. Windows多网卡上网设置

    http://blog.tianya.cn/blogger/post_show.asp?BlogID=1566293&PostID=12984307