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

本题利用BST的特性来用DP求解。由于BST的性质,所以root左子树的node全部<root。而右子树的node全部>root。

左子树 = [1, j-1], root = j, 右子树 = [j+1, n]

dp[n] = sum(dp[j - 1] * dp[n - j])

 def numTrees(self, n):
"""
:type n: int
:rtype: int
"""
dp = [0]*(n+1)
dp[0] = 1
dp[1] = 1 for i in range(2, n+1):
for j in range(1,i+1):
dp[i] += dp[j-1]*dp[i-j] return dp[n]

Leetcode 86. Unique Binary Search Trees的更多相关文章

  1. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

  2. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  3. Java for LeetCode 095 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] 95. Unique Binary Search Trees II 唯一二叉搜索树 II

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

  5. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  6. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

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

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

  8. [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  9. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

随机推荐

  1. Discuz! X2.5判断会员登录状态及外部调用注册登录框

    Discuz! X2.5判断会员登录状态及外部调用注册登录框 有关discuz论坛会员信息,收集的一些资料: 用dedecms+discuz做了个门户加论坛形式的网站,但是dedecms顶部目前只能q ...

  2. Java数据类型和变量

    Java中存在2种数据类型,下面我们来详解一下: 基本数据类型: 引用数据类型: 可以用一张表来记录: 基本数据类型 整型 byte:1个字节8位,取值范围为:[-128, 127],直接写值:(by ...

  3. 布局 - layout

    示例 <div id="cc" class="easyui-layout" style="width:600px;height:400px;&q ...

  4. [转]各种有用的PHP开源库精心收集

    FROM : http://my.oschina.net/caroltc/blog/324024 1.html2ps and html2pdf    下载地址: http://www.tufat.co ...

  5. [py]特殊函数+文件保护

    1函数的好处 2函数的全局变量和局部变量 3,包和文件夹的区别 4,__name__ __file__ __doc__ #判断是否为主程序 if __name__=='__main__': pass ...

  6. QT QT练习一

    界面中通过三个 QLineEdit控件,一个QPushButton实现+ - * /四则运算,点击pushbutton后将运算结果显示在QLabel控件上. #ifndef WIDGET_H #def ...

  7. TinyFrame升级之五:全局缓存的设计及实现

    在任何框架中,缓存都是不可或缺的一部分,本框架亦然.在这个框架中,我们的缓存分为两部分:内存缓存和单次请求缓存.简单说来,就是一个使用微软提供的MemoryCache做扩展,并提供全局唯一实例:另一个 ...

  8. Java 集合与队列的插入、删除在并发下的性能比较

    这两天在写一个java多线程的爬虫,以广度优先爬取网页,设置两个缓存: 一个保存已经访问过的URL:vistedUrls 一个保存没有访问过的URL:unVistedUrls 需要爬取的数据量不大,对 ...

  9. .NET技术在中国为什么老被人嫌弃

    这个话题有点自黑的意思,我从.NET 1.1开始玩.NET,到现在已经11年了,我是看着.NET成长起来,在中国壮大的,也见证了近几年.NET被各种嫌弃,其实说到底还是中国的架构师太少,我是说真正懂行 ...

  10. JavaScript中call,apply,bind方法的总结。

    why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...