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. Haar-like特征

    参考文献: [1]Viola P, Jones M. Rapid object detection using a boosted cascade of simple features[C]//Com ...

  2. Oracle计算时间函数(对时间的加减numtodsinterval、numtoyminterval) (转)

    原文来自:http://blog.itpub.net/756652/viewspace-697256/ 11g interval分区,按天分区,需要用到函数numtodsinterval.   cre ...

  3. php 读取网页源码 , 导出成txt文件, 读取xls,读取文件夹下的所有文件的文件名

    <?php // 读取网页源码$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLO ...

  4. 《Java应用程序(Application)》

    在编写Java应用程序(Application)时可以这样: 1,定义包名. 2, 导入相关的包. 3, 定义一个类. 4,定义相关变量. 5,定义构造函数.(在构造函数内调用init()方法和add ...

  5. Python中文问题(转)

    在本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854: 2. UTF-8,E59388: 3. GBK,B9FE. 一.python ...

  6. C++ Daily 《5》----虚函数表的共享问题

    问题: 包含一个以上虚函数的 class B, 它所定义的 对象是否共用一个虚函数表? 分析: 由于含有虚函数,因此对象内存包含了一个指向虚函数表的指针,但是这个指针指向的是同一个虚函数表吗? 实验如 ...

  7. Python常用函数、方法、模块记录

    常用函数: 1.pow():乘方 2.abs():绝对值 3.round():四舍五入 4.int():转换为整数 5.input():键盘输入(会根据用户的输入来做类型的转换) raw_input( ...

  8. Maven项目pom.xml文件详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. 传智播客JavaWeb day10-jdbc操作mysql、连接数据库六大步骤

    第十天主要讲了jdbc操作mysql数据库,包括连接数据库六大步骤(注册数据库驱动.获得连接对象connetion.生成传输器stament.执行查询获得ResultSet.遍历结果集.关闭资源).介 ...

  10. NSMutableDictionary中 setValue和setObject的区别

    对于- (void)setValue:(id)value forKey:(NSString *)key;函数 官方解释如下 Send -setObject:forKey: to the receive ...