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 ...
随机推荐
- 内核input子系统分析
打开/driver/input/input.c 这就是input代码的核心 找到 static int __init input_init(void) { err = class_register(& ...
- 详解Android中AsyncTask的使用
在Android中实现异步任务机制有两种方式,Handler和AsyncTask. Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更 ...
- SQLserver技巧
(1) SQL标记 连接连个表然后用 DATA COMPAREDATA进行区分select 'DATA ' ,'列名1','列名2','列名3' from 表 union select 'COM ...
- .Net读取Excel文件时丢失数据的问题 (转载)
相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...
- jquery中的children()和contents()的区别
1.children()只会返回元素节点 2.contents()还可以返回文本节点
- hdu 2062
ps:11版的最后一题...是个递推题...比如n=5,推出首数字后,n--,继续找下一个 代码: #include "stdio.h" ]; ]; int main(){ lon ...
- IOS的Crash情况在Crashlytics平台上统计解决方案的一点遗憾(截止到2015年6月14日)
平台针对特定版本的monkey操作后数量统计,按时间段定时去获取,最后根据操作批次出具分析报告: 问题是crashlytics平台仅提供一个BS登录查看WEB后台,所以无法通过API或者DB去直接获取 ...
- NSDateFormatter遇到无法转换的问题
NSDateFormatter并不是万能的,并不是给出什么字符串都能转遍为NSDate类型,所转换的格式必须必须和你给出的格式想对应 比如说:NSString *dateStr = @"20 ...
- Eclipse/JavaWeb (一)三大框架之struts框架 持续更新中...
最近主要把javaweb的三大框架过一遍. (一)发展历史 传统的Java Web应用程序是采用JSP+Servlet+Javabean来实现的,这种模式实现了最基本的MVC分层,使得程序分为几层,有 ...
- Container With Most Water -- LeetCode 11
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...