【二叉查找树】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 ...
随机推荐
- 生成n个元素的全排列 C实现
近期在准备复习算法设计的考试,下边记录一些,看笔记时突然想到的解法. 问题是这种 用递归实现 n 个元素的全排列. 当时老师给出的解答是 假定第i个元素 ri 放在首位,于是 f(r1,r2,-,rn ...
- 【BZOJ2081】[Poi2010]Beads hash+调和级数
[BZOJ2081][Poi2010]Beads Description Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子串), ...
- 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数
题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...
- hdu 2036 改革春风吹满地【求多边形面积模板】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2036 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- [Android]豆瓣FM离线数据
离线目录结构: /sdcard/Android/data/com.douban.radio下 ./cache/fileCaches: 离线音乐歌词(lyric) ./cache/images: 离线音 ...
- Is this its limit?
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(c ...
- php解析xml文件为数组
$xml = simplexml_load_file($fullfilename); $arr = json_decode(json_encode($xml),true); echo "&l ...
- PHP开发环境搭建(转载)
转载自:http://blog.csdn.net/rosetta/article/details/53967215 前言 最近学了n种语言,学每种语言的套路无非就是先搭建一个开发环境,再找本书或者 ...
- [luogu3767]膜法
[luogu3767]膜法 luogu 神仙题 线段树分治+带权并查集 把每个操作看成点 首先这个操作的结构是一棵树 你发现每个点的对它的子树产生影响 我们可以想到用dfn序把它转成一段区间用线段树分 ...
- Linux项目部署发布
Linux项目部署发布 1.部署环境准备,准备python3和虚拟环境解释器,virtualenvwrapper pip3 install -i https://pypi.douban.com/sim ...