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 唯一二叉搜索树的更多相关文章

  1. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...

  4. [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 ...

  5. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  9. [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 ...

随机推荐

  1. 配置redis, make的时候: zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录

    今天正在centos7.3里面配置redis3.0, 结果make的时候抛出编译中断 CC adlist.o In file included from adlist.c:34:0: zmalloc. ...

  2. Class<T>

    首先,什么是类类型? 可见: https://www.cnblogs.com/yanze/p/9717658.html Class<T>即T的类类型 如何获取Class<T>? ...

  3. MSM8937系统启动流程【转】

    本文转载自:https://blog.csdn.net/chenzhen1080/article/details/54945992?utm_source=blogxgwz8 1 Boot Addres ...

  4. win7 64位debug解决方法

    1.下载win 64位的DOSBox,如DOSBox0.74: 2.下载win 32 debug.exe,并复制到调用的目录,如d盘根目录d:\ 3.安装DOSBox,并运行:如下图: 4.键入命令: ...

  5. luoguP2572 [SCOI2010]序列操作

    题目&&链接 反正数据都是一样的,luogu比较友好 luogu bzoj lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变 ...

  6. Centos用yum方式安装nodejs和npm

    要通过 yum 来安装 nodejs 和 npm 需要先给 yum 添加 epel 源 ##添加 epel 源 rpm -ivh http://download.fedoraproject.org/p ...

  7. icpc 2017北京 J题 Pangu and Stones 区间DP

    #1636 : Pangu and Stones 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In Chinese mythology, Pangu is the fi ...

  8. BZOJ4415: [Shoi2013]发牌 树状数组+二分

    Description 假设一开始,荷官拿出了一副新牌,这副牌有N张不同的牌,编号依次为1到N.由于是新牌,所以牌是按照顺序排好的,从牌库顶开始,依次为1, 2,……直到N,N号牌在牌库底.为了发完所 ...

  9. java的基本数据类型默认值

    这里就举int类型 默认值在类实例化,也就是对象中才有默认值0,或者是静态变量. 1.先看局部变量使用(不行,报错) 2.静态变量 3.类非静态属性

  10. YOLOv2-darknet 内容解析

    目录 YOLOv2-darknet 内容解析 1. 改进之处 2. Better 3. Faster 4. Stronger 5. 总结 reference YOLOv2-darknet 内容解析 1 ...