LEETCODE —— 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
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)
LEETCODE —— Unique Binary Search Trees [动态规划]的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 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 ...
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- [Leetcode] Unique binary search trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- leetcode -- Unique Binary Search Trees todo
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- js拖拽效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JAVA时间格式转换大全
import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 ...
- Linux网络编程(简单的时间获取服务器)
1.一个简单的服务器时间获取程序 服务器和客户端采用UDP通信的方式,来编写一个简单的时间获取应用. 把过程大致理顺一下,首先是服务器端的编写,使用的是迭代的方式,没有并发 先创建一个socket而后 ...
- View通用
1.计算view尺寸 ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeO ...
- H5的FormData对象完成ajax上传文件multiFile
最近工作中需要完成,ajax上传图片,可是input file +ajax是无法完成的: 于是寻找了许久,发现了H5 的一个对象FormData 使用方法如下: HTML: <form id=& ...
- 《嫌疑人X的献身》书评
<嫌疑人X的献身>是日本著名推理小说作家东野圭吾的代表作之一.1985年东野圭吾以一本<放学后>出道,出道初期善于写精巧细致的本格推理,后期文笔愈发老辣.简练.2005年东野圭 ...
- 一篇介绍java与js操作cookie的
http://blog.csdn.net/xuweilinjijis/article/details/8651188
- NLog的使用
1.项目添加NLog的项目引用: 2.将工程的framework框架改为3.5或4.0,将配置文件[NLog.config]拷贝至运行的bin/(release/debug)路径下 3.项目中代码使用 ...
- C语言程序设计第9堂作业
一.本次课主要内容: 本章将散布在前五章中的数据类型和表达式等内容做了归纳性的汇总,本次课需要掌握以下两个方面: (1)本次课通过从数据在计算机内的存储格式入手,介绍整型.字符型.单精度和双精度实型四 ...
- JSP自定义标签——调用数据库(通过id号搜索相关信息)
一.创建新表(假设在master数据库下新建) 二.连接数据库 开始-->控制面板-->管理工具-->数据源-->系统DSN-->添加-->SQL Server-- ...