leetcode94 不同的二叉搜索树

solution 1:**动态规划
class Solution {
public:
int numTrees(int n) {
vector<int> g={1,1,2};
for(int i=3;i<=n;i++){
int sum=0;
for(int j=1;j<=i;j++){
sum+=g[j-1]*g[i-j];
}
g.push_back(sum);
}
return g[n];
}
};
solution 2:**数学演绎法,求卡塔兰数
直接利用公式进行计算:c(i+1)=c(i)(2(2n+1)/(n+2));
class Solution {
public:
int numTrees(int n) {
if(n<=1) return n;
int c=1;
for(int i=1;i<n;i++){
long a=long(c)*long(4*i+2);
long b=i+2;
c=a/b;
}
return c;
}
};
leetcode94 不同的二叉搜索树的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 ...
随机推荐
- 三次样条插值 cubic spline interpolation
什么是三次样条插值 插值(interpolation)是在已知部分数据节点(knots)的情况下,求解经过这些已知点的曲线, 然后根据得到的曲线进行未知位置点函数值预测的方法(未知点在上述已知点自变量 ...
- 【异常】azkaban.executor.ExecutorManagerException: No active executors found
1 azkaban启动异常 没有找到活动的executors,需在MySQL数据库里设置端口为12321的executors表的active为1 update azkaban.executors ...
- 3.Bacula Client安装配置
1. Bacula Client安装配置 1.1. linux客户端安装 1.1.1. 安装依赖包 yum install libacl libacl-devel 1.1.2. Clien ...
- Django_03_后台管理
后台管理 站点分为内容发布和公共访问两部分 内容发布的部分由网站的管理员负责查看.添加.修改.删除数据,开发这些重复的功能是一件单调乏味.缺乏创造力的工作,为此,Django能够根据定义的模型类自动地 ...
- 牛客练习赛53 E 老瞎眼 pk 小鲜肉 (线段树,思维)
链接:https://ac.nowcoder.com/acm/contest/1114/E来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- loj2587 「APIO2018」铁人两项[圆方树+树形DP]
主要卡在一个结论上..关于点双有一个常用结论,也经常作为在圆方树/简单路径上的良好性质,对于任意点双内互不相同的三点$s,c,t$,都存在简单路径$s\to c\to t$,证明不会.可以参见clz博 ...
- js中回调函数(callback)的一些理解
前言 我个人在学习Node.js相关知识时遇到了回调函数这个概念,虽然之前已经在c,c++等编程语言中用到过它,但还一直未对其机制有深入了解,这次就来好好谈一下它. 概念理解 百度对它的解释是回调函数 ...
- hivesql之 table名 with as 转储
可能某个子查询在多个层级多个地方存在重复使用的情况,这个时候我们可以使用 with as 语句将其独立出来,极大提高SQL可读性,简化SQL~ 注:目前 oracle.sql server.hive等 ...
- awk的妙用
终端形式 有人说awk的优势在于可以个性化输出命令,这么说来太抽象了,假如我们查看占用6379端口的进程信息. lsof -i: 输出结果: COMMAND PID USER FD TYPE DEVI ...
- springboot整合easyexcel实现Excel导入导出
easyexcel:快速.简单避免OOM的java处理Excel工具 Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套 ...