[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 ...
随机推荐
- PHP-ThinkPHP5砍价活动相关设计
近期我们公司项目里陆陆续续有很多为了招引新用户的活动推出,砍价的活动由我来负责,我们的项目是在微信浏览器里供用户浏览访问. 大概描述:进入砍价活动列表页选择有意向的商品,用户点击商品图片可以看到WEB ...
- c++性能之对象与指针性能比较、以及java与c++性能对比实测
为了更加直观的比较,好吧,我们选择以对象的初始化并add到list为例子. 首先,定义object如下: #include <string> #pragma once using name ...
- 20145314郑凯杰《网络对抗技术》实验9 web安全基础实践
20145314郑凯杰<网络对抗技术>实验9 web安全基础实践 一.实验准备 1.0 实验目标和内容 Web前端HTML.能正常安装.启停Apache.理解HTML,理解表单,理解GET ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- windows服务与自启动程序的区别(转载)
转载:http://blog.csdn.net/anddy926/article/details/8464142 在客户端服务器项目实践中,作为服务端必须保持程序的24小时不间断运行,需要做一个监控, ...
- 深入理解Java面向对象三大特性 封装 继承 多态
1.封装 封装的定义: 首先是抽象,把事物抽象成一个类,其次才是封装,将事物拥有的属性和动作隐藏起来,只保留特定的方法与外界联系 为什么需要封装: 封装符合面向对象设计原则的第一条:单一性原则,一个类 ...
- UVa 10118 免费糖果(记忆化搜索+哈希)
https://vjudge.net/problem/UVA-10118 题意: 桌上有4堆糖果,每堆有N颗.佳佳有一个最多可以装5颗糖的小篮子.他每次选择一堆糖果,把最顶上的一颗拿到篮子里.如果篮子 ...
- Ubuntu14.04 libboost_program_options.so.1.54.0: cannot open shared object file: No such file or directory
macname@ubuntu:~/Desktop$ roslaunch blackrospack: error : cannot open shared object file: No such fi ...
- c++ 返回指定元素连续相等的位置索引(equal_range)
#include <iostream> // cout #include <algorithm> // equal_range, sort #include <vecto ...
- myEclipse 下配置多个Tomcat
1.进入perfomance 2. 进入server 右键点击configure server connector 3. 切换到 “Arguments” 面板,这里有 一个启动参数,就是修改一下路径 ...