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 ...
随机推荐
- android工程中menu的使用总结
android的Menu使用 转自:thinkYeah的博客 1.普通的Menu 先来看看最简单的Menu怎样实现. 在主Activity中覆盖onCreateOptionsMenu(Menu men ...
- DOM4J的使用
http://blog.csdn.net/redarmy_chen/article/details/12969219 http://blog.csdn.net/wlbing0625/article/d ...
- 2014年7月份第1周51Aspx源码发布详情
QF万能视频播放器源码 2014-6-30 [VS2010]本源码是一个万能视频播放器源码.可实现各种格式的影片播放功能. 1.点击[开始]按钮,弹出窗口,选择影片路径,确定后即可播放.可拖拽滚 ...
- 一些Layout的坑。坑死我自己了
iOS这个东西,初学感觉,还好还好,然后一年之后再来修复一下初学的时候的代码,我只是感觉头很晕- - 别扶我. AutoLayout的坑,明明以前都没有的!!!升了iOS10就突然发现了这个坑,其实也 ...
- Go语言并发编程示例 分享(含有源代码)
GO语言并发示例分享: ppt http://files.cnblogs.com/files/yuhan-TB/GO%E8%AF%AD%E8%A8%80.pptx 代码, 实际就是<<Go ...
- 回头再看N层架构(图解)
不知不觉来博客园已经快两半了,时间过的真快. 这次的目标是再回顾一下传统的N层架构并且分析一下在DDD中的N层架构. 一.先来看一看传统的N层架构 N-层架构的出现,主要是由于观注点的分离而产生,这三 ...
- 微信支付curl出错及错误码解决方案
1. curl错误码6 出现场景 PHP Fatal error: Uncaught exception 'WxPayException' with message 'curl出错,错误码:6' in ...
- 关于Webstorm的一些配置
一:代码缩略图插件:CodeGlance: 二:皮肤与主题使用的sublime的系列风格. 三:实现内嵌JS代码的高亮显示.https://segmentfault.com/q/10100000024 ...
- codeforces 723D(DFS)
题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...
- re模块详解
#!/usr/bin/env python #-*- coding:UTF-8 -*- ##################################################### # ...