【Leetcode】【Medium】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
解题:
单纯的看做一个数列题:1, 2, 5, 14, 42, 132, ... , 求通项。
这是一个典型的卡塔兰数,中国人明安图比卡塔兰早100多年就发现了这种数列,其实更应该叫明安图数。
程序实现不用这么做,不过还是给出通项公式:f(n) = (2n)! / [(n+1)! * n!];
在原列前补充一个值为1的0位:1, 1, 2, 5, 14, 42, 132, ...
则从新数列第3个数(2)开始,f(n) = f(0) * f(n-1) + f(1) * f(n-2) + ... + f(n-1) * f(0);
代码:
class Solution {
public:
int numTrees(int n) {
if (n == )
return ;
vector<int> res(n+, );
res[] = res[] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) {
res[i] += res[j] * res[i--j];
}
}
return res[n];
}
};
Leetcode测试集中没有考察n为0的情况,我认为n为0时还是有意义的,应该返回0。
【Leetcode】【Medium】Unique Binary Search Trees的更多相关文章
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- 【LeetCode】96. Unique Binary Search Trees (2 solutions)
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- wiz笔记
分享本地的wiz笔记到网上 , 成为博客 博客园 http://www.cnblogs.com/您的博客名称/services/metablogapi.aspx // region 图片里面的字段:从 ...
- CSAPP阅读笔记-栈帧-来自第三章3.7的笔记-P164-P176
1.基本结构: 如上图所示,是通用的栈帧结构.大致分两块,调用者函数P和被调用者函数Q. 对P来说,要做的工作是把传递参数中多于6个的部分压栈,随后把Q返回时要执行的下一条指令的地址压栈. 对Q来说, ...
- ubuntu16.04 安装python3.6
https://www.cnblogs.com/yjlch1016/p/8641910.html
- MongoDB实战开发
[目标]:本文将以实战的形式,向您展示如何用C#访问MongoDB,完成常见的数据库操作任务, 同时,也将介绍MongoDB的客户端(命令行工作模式)以及一些基础的命令. [说明]:MongoDB是什 ...
- BAE+Python+Django+Wechatpy+Baidu weather api +微信订阅号 = 实现微信查询天气
最近想在微信上面实现天气查询,分两个步骤: 1.被动回复:输入天气.xx天气获取天气情况 2.主动推送:每天定时推送天气(针对24小时内产生交互的人) 目前已经实现第一个步骤,如下: 现将实现此功能环 ...
- 1 复习ha相关 + weekend110的hive的元数据库mysql方式安装配置(完全正确配法)(CentOS版本)(包含卸载系统自带的MySQL)
本博文的主要内容是: .复习HA相关 .MySQL数据库 .先在MySQL数据库中建立hive数据库 .hive的配置 以下是Apache Hadoop HA的总结.分为hdfs HA和yarn HA ...
- python 冒泡和快排,不多说
#-*-coding:utf8-*- import random a=[] b=[] def init_array(): for i in range(10000): v = random.randi ...
- 带有Apache Spark的Lambda架构
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 目标 市场上的许多玩家已经建立了成功的MapReduce工作流程来每天处理以TB计的历史数据.但是谁愿意等待24小时才能获得最新的分析结果? ...
- <数据挖掘导论>读书笔记4--其他分类技术
1.基于规则的分类器 2.最近邻分类器 3.贝叶斯分类器 4.人工神经网络 5.支持向量机 6.组合方法 7.不平衡类问题 8.多类问题
- [转] Entity Framework添加记录时获取自增ID值
本文转自:http://blog.csdn.net/educast/article/details/8632806 与Entity Framework相伴的日子痛并快乐着.今天和大家分享一下一个快乐, ...