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. 内核input子系统分析

    打开/driver/input/input.c 这就是input代码的核心 找到 static int __init input_init(void) { err = class_register(& ...

  2. 详解Android中AsyncTask的使用

    在Android中实现异步任务机制有两种方式,Handler和AsyncTask. Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更 ...

  3. SQLserver技巧

    (1) SQL标记  连接连个表然后用 DATA  COMPAREDATA进行区分select 'DATA ' ,'列名1','列名2','列名3' from  表 union select 'COM ...

  4. .Net读取Excel文件时丢失数据的问题 (转载)

    相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...

  5. jquery中的children()和contents()的区别

    1.children()只会返回元素节点 2.contents()还可以返回文本节点

  6. hdu 2062

    ps:11版的最后一题...是个递推题...比如n=5,推出首数字后,n--,继续找下一个 代码: #include "stdio.h" ]; ]; int main(){ lon ...

  7. IOS的Crash情况在Crashlytics平台上统计解决方案的一点遗憾(截止到2015年6月14日)

    平台针对特定版本的monkey操作后数量统计,按时间段定时去获取,最后根据操作批次出具分析报告: 问题是crashlytics平台仅提供一个BS登录查看WEB后台,所以无法通过API或者DB去直接获取 ...

  8. NSDateFormatter遇到无法转换的问题

    NSDateFormatter并不是万能的,并不是给出什么字符串都能转遍为NSDate类型,所转换的格式必须必须和你给出的格式想对应 比如说:NSString *dateStr = @"20 ...

  9. Eclipse/JavaWeb (一)三大框架之struts框架 持续更新中...

    最近主要把javaweb的三大框架过一遍. (一)发展历史 传统的Java Web应用程序是采用JSP+Servlet+Javabean来实现的,这种模式实现了最基本的MVC分层,使得程序分为几层,有 ...

  10. Container With Most Water -- LeetCode 11

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...