【二叉查找树】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 ...
随机推荐
- .Net 开发Windows Service
1.首先创建一个Windows Service 2.创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释: using Syste ...
- html5-补充知识
原理 html5+开发app的原理大概是这样: html页面负责内容: ui负责页面样式: js负责调用原生app方法. html5 html5这部分负责页面,也就是app中你看到的东西,大概的架构和 ...
- Linux下apache安装php
php 1.下载解压 cd /usr/local/src wget http://mirrors.sohu.com/php/php-5.6.9.tar.gz tar zxvf php-5.6.9.ta ...
- META-INF中的INF的意思
1 META是元的意思,比如meta data,元数据. 2 什么是meta data 元数据就是描述其它数据的数据,比如web page中的meta data,包括关键字,对该网页的描述等等. 3 ...
- 洛谷 P3674 小清新人渣的本愿
想看题目的戳我. 我刚开始觉得这道题目好难. 直到我从Awson大佬那儿了解到有一个叫做bitset的STL,这道题目就很容易被解开了. 想知道这个神奇的bitset的戳我. 这个题目一看就感觉是莫队 ...
- java堆分析神器MAT
Memory Analyzer(MAT) 基于Eclipse的软件 http://www.eclipse.org/mat/
- Django之restframework2视图三部曲
视图三部曲 下面我来来看restframework是如何将冗余的代码一步步的进行封装. 这里主要用到的是多继承 第一步mixin类编写视图 AuthorModelSerializer: class A ...
- Linux使用yum安装rpm包
1.yum其实管理的也是rpm包,只不过依赖什么的都自己做了2.yum在有的linux版本是收费的,但是CentOS是免费的3.yum一般意义上是需要联网的,即:使用网络yum源 a.yum源配置文件 ...
- 打开或者 关闭 php 的错误报告
一般线上的环境,我会 php的报错信息屏蔽掉,php.ini 设置的办法 如下: display_errors = Off error_reporting = E_ALL 在代码中,可以这样~~: e ...
- mysql主从复制(linux下)
转至:http://369369.blog.51cto.com/319630/790921 怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1. ...