【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】
当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性:
以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成。
我们假定f(i)为以[1,i]能产生的Unique Binary Search Tree的数目,则
- 如果数组为空,毫无疑问,只有一种BST,即空树,f(0)=1。
- 如果数组仅有一个元素1,只有一种BST,单个节点,f(1)=1。
- 如果数组有两个元素1,2,那么有如下两种可能:
2 1
那么分析可得
f(2) = f(0) * f(1),1为根的情况
+ f(1) * f(0),2为根的情况
再看一看3个元素的数组,可以发现BST的取值方式如下:
f(3) = f(0) * f(2),1为根的情况
+ f(1) * f(1),2为根的情况
+ f(2) * f(0),3为根的情况
所以,由此观察,可以得出f的递推公式为:
f(i) = f(k - 1) * f(i - k) ps. k = 1 ... i
由此问题划归为一维动态规划。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定一个n,问有多少个不同的二叉查找树,使得每个节点的值为 1...n?
例如,
给定n=3,这里一共有5中不同的二叉查找树。
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test.cpp:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream>
#include <vector> using namespace std; int numTrees(int n) //迭代开始 int main() |
结果输出:
【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】的更多相关文章
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- Unique Binary Search Trees II leetcode java
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- 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 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 2014年3月1日 Start && Unique Binary Search Trees
早上和面试官聊天, 才发现自己的基础下降的有点厉害, 过去那个飘逸写程序的小青年, 如今有点走下坡路了. 可惜我不服,所以要开始做题,把水平恢复上来,能力是最重要的. 最近在做LeetCodeOJ的题 ...
- 96. Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
随机推荐
- 鸟哥的Linux私房菜-----6、文件与文件夹管理
- Solr6.5创建core
首先在solrhome(solrhome的路径和配置见中solr的web.xml)http://www.cnblogs.com/paulversion/p/6827949.html 中创建mycore ...
- Python 推导式、迭代器、生成器、模块和包
一.推导式 (一).列表推导式(集合推导式也同理于此) 利用列表推导式,取出1-20内所有偶数 li = [i for i in range(1, 21) if i % 2 == 0] # 如果只有一 ...
- MySQL 下 ROW_NUMBER / DENSE_RANK / RANK 的实现
原文链接:http://hi.baidu.com/wangzhiqing999/item/7ca215d8ec9823ee785daa2b MySQL 下 ROW_NUMBER / DENSE_RAN ...
- 【BZOJ4010】[HNOI2015]菜肴制作 拓扑排序
[BZOJ4010][HNOI2015]菜肴制作 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高 ...
- POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 中国移动OneNet平台上传GPS数据JSON格式
最终目的输出 POST /devices/3225187/datapoints HTTP/1.1 api-key: R9xO5NZm6oVI4YBHvCPKEqtwYtMA Host: api.hec ...
- ARM汇编学习笔记
ARM RISC (Reduced Instruction Set Computers) X86 CISC (Complex Instruction Set Computers) ...
- iOS项目中获取验证码倒计时及闪烁问题解决方案
-(void)startTime{ __block int timeout= 59; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queu ...
- Linux电源管理(4)-Power Manager Interface【转】
本文转载自:http://www.wowotech.net/pm_subsystem/pm_interface.html 1. 前言 Linux电源管理中,相当多的部分是在处理Hibernate.Su ...