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 ...
随机推荐
- 处理OSX创建的U盘, 删除EFI分区
1. 运行 diskpart 2. list disk 3. 根据列出的硬盘, select disk [编号] 4. clean 5. exit 然后再创建分区和格式化
- C# socket UDPの异步链接
基于C#的UDP异步连接,在本机上通过测试
- ASP.NET MVC铵钮Click后下载文件
本篇Insus.NET练习的是FilePathResult和FileStreamResult操作.本篇也算是<如何把Json格式字符写进text文件中>http://www.cnblogs ...
- Protocol in Objective-C
Objecttive-C Protocal 相似 Java Interface
- iOS关于TableViewController和CollectionViewController中self.view心得记录
之前写代码,不喜欢记录,导致很多做过的功能,时间久了都遗忘了. 以后要勤记录~~~ 一丶首先说一下TableViewController 大家都知道,如果直接创建一个继承于TableViewContr ...
- 读懂IL代码就这么简单(二)
一 前言 IL系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...
- ContentProvider详解
作用:把一个App中的数据库通过Url的形式共享出来,供其他App使用. 首先在App1中创建一个数据库,用SQLiteOpenHelper public class MyOpenHelper ext ...
- Linux 安装mysql+apache+php
安装mysql 1. yum install mysql mysql-server 2. 修改mysql密码 >use mysql >update user set passwor ...
- extJS 创建类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- YY游戏私有云平台实践 (转自InfoQ )
作者 风河 发布于 2016年1月13日 | 讨论 编者按:YY游戏的页游早在2013年就在云平台上运行,其Cloud 1.0已经支撑几十万的同时在线用户.日前,YY游戏云平台进行了Cloud 2 ...