Leetcode 86. Unique Binary Search Trees
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的更多相关文章
- [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 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 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 ...
- [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 ...
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- shipyard安装
1.Start an data volume instance of RethinkDB: # docker run -it -d --name shipyard-rethinkdb-data \ - ...
- Debian 8(Jessie) 安装自带Mysql
执行命令 sudo apt-get install mysql-server 这会把mysql-client也装上, 版本都是5.5. 安装过程中会提示你输入两遍root口令. 用ps aux|gre ...
- 在SecureCRT中使用rz和sz传输文件
首先检查Centos中有没有安装 lrzsz sudo yum install lrzsz 使用yum install的时候碰到一个问题, 不知道是否和虚拟机环境有关 Existing lock /v ...
- 039医疗项目-模块四:采购单模块—采购单的创建-----------Dao层,service层,Acion层
我们根据数据库里面的表格:生成对应的Mapper接口文件和...Mapperxml文件: YYCGD:采购单模板表. YYCGD2014:采购单动态生成表,由存储过程生成. YYCGDMX:采购单明细 ...
- kindeditor编辑器里面 filterMode为false时候,允许输入任何代码
kindeditor编辑器里面 filterMode为false时候,允许输入任何代码
- redis 学习笔记(2)-client端示例代码
redis提供了几乎所有主流语言的client,java中主要使用二种:Jedis与Redisson 一.Jedis的使用 <dependency> <groupId>redi ...
- struts2: config-browser-plugin 与 convention-plugin 学习
struts2被很多新手诟病的一个地方在于“配置过于复杂”,相信不少初学者因为这个直接改投Spring-MVC了.convention-plugin. config-browser-plugin这二个 ...
- .clear 万能清除浮动
html body div.clear, html body span.clear { background: none; border: 0; clear: both; display: block ...
- background-position 50% 50%是如何计算的
background-position:value1 value2 value1和value2的值可以值绝对值也可以是百分数,大部分值都很好理解,但是50% 50%这两个值是如何计算的呢? 图片水平和 ...
- 使用ContentProvider进行应用程序间的数据交互
什么是ContentProvider: ContentProvider用来管理数据的访问规则.它允许你的应用程序向外界暴露需要被访问的数据. 是Android的四大组件之一. ContentProvi ...