[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
题意:给定数n,二叉树的结点的值分别为1,2....n。问能组成多少种不同的二叉搜索树。
二叉搜索树的性质为:在任一结点r的左(右)子树中,所有结点(若存在)均小于(大于)r。更一般性的特点是:任何一棵二叉树是二叉搜索树,当且仅当其中序遍历序列单调非降。
恩,一看,一想不会,好吧,又要找大神们了。
方法一:递归
思路:空树和只有根节点时,也为BST。对于一点i,当其为根节点时,左子树的节点的个数为i-1,(为1,...i-1),右子树的个数为n-i(为,i+1,...n)。对一个根来说,唯一二叉树的个数为左子树结点的个数乘以右子树的个数。而根节点可以从1到n 中选择。
class Solution {
public:
int numTrees(int n)
{
if(n<=) return ;
int sum=;
for(int i=;i<=n;++i)
sum+=numTrees(i-)*numTrees(n-i); return sum;
}
};
方法二:
还有大神说这是Catalan Number卡特兰数的一个例子。卡特兰数的的递推公式:
可以使用动态规划解决问题。维护向量sumNode,sumNode(i)为结点个数为i时,唯一二叉搜索树的个数。和这题相对应的意义,可以写出n较小的情况。
class Solution {
public:
int numTrees(int n)
{
vector<int> sumNode(n+,);
sumNode[]=;
sumNode[]=; for(int i=;i<=n;++i)
for(int j=;j<i;++j) //j符合条件时,最大为i-1,对照公式
sumNode[i]+=sumNode[j]*sumNode[i-j-]; return sumNode[n];
}
};
[Leetcode] Unique binary search trees 唯一二叉搜索树的更多相关文章
- [LeetCode] 96. 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 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
随机推荐
- c++不自动生成相关函数比如赋值、拷贝函数
默认情况下,如果没有明确声明某些函数比如赋值.拷贝函数,c++会自动生成这些函数,通常他们是对成员进行by-value拷贝,有些时候,赋值.拷贝对象并无什么意义或者不合理,比如对于socket或者th ...
- 自定义redis序列化工具
redis一个优点就是可以将数据写入到磁盘中. 我们知道写入磁盘的数据实际上都是以字节(0101这样的二进制数据)的形式写入的. 这意味着如果我们要将一个对象写入磁盘,就必须将这个对象序列化. jav ...
- [微信开发] - 微信支付 JSAPI 形式
微信官方的JSAPI文档 微信官方的JSAPI支付SDK与DEMO下载 查看JSAPI的API可以从这里看 下载了支付DEMO其实有些地方不对的,比如如果做沙盒测试的时候,需要使用getsignkey ...
- 切换tab页时,tab页中的echart变形问题
本文为博主原创,未经允许,不得转载: 在两个tab页中,分别展现了两个echart图表,同样的格式与写法,但只有在默认选中的tab页中的图表显示的是正常的, 但进入另一个tab页中时,图表则产生了变形 ...
- Ubuntu Eclipse ns3编译中 遇到的OSError 系列问题
问题1:Permission denied 解决方法:修改文件权限,利用 chmod 命令 修改在 /home/wasdns/workspace/MyNS3_Mac/ns-3.25 (eclipse工 ...
- 一些WGS健康体检网站和公司
1.wegen https://www.wegene.com/ 2.阅己基因 http://www.selfgene.com/
- Codeforces Beta Round #17 A.素数相关
A. Noldbach problem Nick is interested in prime numbers. Once he read about Goldbach problem. It sta ...
- 哈希表-java
import java.util.HashMap; import java.util.Iterator; public class JavaHashMap { public static void m ...
- html 绘制矩形轨迹,选中区域
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- listener TNS-01189 问题
-- 启动监听,提示已经启动. [oracle@sh ~]$ lsnrctl start LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 0 ...