leetcode[94] Unique Binary Search Trees
给定n,那么从1,2,3...n总共可以构成多少种二叉查找数呢。例如给定3
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
思路:
我们考虑头结点i,那么所有比i小的都在i的左边,比i大的都在i的右边。也就是以i为开头的是i的左边的可能*i右边的可能,然后遍历i从1到n,所有可能相加就是我们的结果。
由公式 h[n] = h[0]*h[n-1] + h[1]*h[n-1] + ... + h[n-1]*h[0]; 可得如下:
class Solution {
public:
int numTrees(int n) {
if (n == ) return ;
vector<int> ans(n+);
ans[] = ;
ans[] = ;
for (int i = ; i <= n; i++)
for (int j = ; j < i; j++)
{
ans[i] += ans[j]*ans[i-j-];
}
return ans[n];
}
};
其实这是一个卡特兰数,直接用公式C2n选n除以n+1则如下:
class Solution {
public:
int numTrees(int n) {
if (n == ) return ;
long long denominator = , numerator = ;
int cnt = * n;
while(cnt > n) denominator *= cnt--;
while(cnt > ) numerator *= cnt--;
return denominator/numerator/(n+);
}
};
还可以用递归:
class Solution {
public:
int numTrees(int n)
{
return numTrees(,n);
}
int numTrees(int start, int end)
{
if (start >= end)
return ;
int totalNum = ;
for (int i=start; i<=end; ++i)
totalNum += numTrees(start,i-)*numTrees(i+,end);
return totalNum;
}
};
leetcode[94] Unique Binary Search Trees的更多相关文章
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [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] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- dojo/dom源码
dojo/dom源码学习 dojo/dom模块作为一个基础模块,最常用的就是byId方法.除此之外还有isDescendant和setSelectable方法. dom.byId(myId)方法: ...
- java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException
不是一个Exception,而是一个Error,要强制转成Exception类型,就出错了java.lang.NoClassDefFoundError java.lang.RuntimeExcepti ...
- (大数据工程师学习路径)第一步 Linux 基础入门----基本概念及操作
本节联练习主要有: 1.环境介绍 2.常用 Shell 命令及快捷键 3.Linux 使用小技巧 一.Linux 桌面环境介绍 相对于现在的 Windows 系统,UNIX/Linux 本身是没有图形 ...
- 控制执行流程——(Java学习笔记三)
if-else 控制程序流程最基本的形式 格式: if(boolean - expresion){ statement } 或 if(boolean - expresion){ stateme ...
- iOS开发之protocol和delegate
protocol--协议 协议是用来定义对象的属性,行为和用于回调的. 协议中有两个keyword@private和@optional,@private表示使用这个协议必需要写的方法,@op ...
- mysql中国的内容php网页乱码问题
1.更改mysql编码在数据库 character_set_server=utf8 init_connect='SET NAMES utf8' 加入这两行 2.又第一次启动mysql数据库 版权声明: ...
- Unix命令操作
基本命令 [ man 查看 ]--万能命令 1.ls 列出文件 (-al) 2.cd 转换目录 3.mkdir 建立新目录 4.cp 拷贝文件 (-R) 5.rm 删除文件 (-rf) 6.mv 移动 ...
- Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串
原文:Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串 声明xml字符串: string xml = ...
- IOS程序创建view
在IOS程序中创建view有六种方式 首先创建一个GLViewController类,继承UIViewController. 然后进入GLAppDelegate.m,在- (BOOL)applicat ...
- Php设计模式(三):行为型模式part1
原文详见:http://www.ucai.cn/blogdetail/7023?mid=1&f=5 可以在线运行查看效果哦! 在上一篇我们讲了结构型模式,结构型模式是讨论类和对象的结构的.总共 ...