[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 ...
随机推荐
- 编译安装 http
1. 安装 apr http服务依赖 apr和apr-util ,安装 http 前需先安装这两个程序 apr 简介:http://www.cnblogs.com/Alight/p/3997777.h ...
- vc++之stdafx.h
关于stdafx.h的解释,其实蛮多的,在vs中,既然创建c++工程的时候,默认会给生成main.cpp,并且自动包含了stdafx.h,而且stdafx.h不是c++标准的一部分,那么个人认为,理解 ...
- c++性能之map实现性能比较
http://www.cnblogs.com/zhjh256/p/6346501.html讲述了基本的map操作,在测试的时候,发现map的性能极为低下,与java相比相差了接近200倍.测试的逻辑如 ...
- VC++实现获取文件占用空间大小的两种方法(非文件大小)
// GetFileSpaceSize.cpp : Defines the entry point for the console application. // /***************** ...
- 字符编码之间的相互转换 UTF8与GBK(转载)
转载自http://www.cnblogs.com/azraelly/archive/2012/06/21/2558360.html UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 ...
- Python3基础 os listdir curdir 查看当前工作目录的所有文件的名字
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- ubuntu12 root账户自动登录
Ubuntu为了系统安全,root帐号的密码是随机的,如果临时需要提升至root权限以执行一些命令,需要使用sudo命令.产线上有几台使用Ubuntu的机器,因为使用者不固定,并且执行程序时需要使 用 ...
- 【附3】springboot源码解析 - 构建SpringApplication
package com.microservice.framework; import org.springframework.boot.SpringApplication; import org.sp ...
- 【Django】【六】接口自动化测试框架
我的源码地址:https://github.com/woshixiaoyu202017/djangoTest 详细构建步骤如下 1. 生成新的测试数据库的数据库表结构guest_test 2. 数据库 ...
- python 元组中元组转为字典
#create a tuple tuplex = ((, , "r")) print(dict((y, x) for x, y in tuplex))