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 不同的二叉搜索树的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. [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 ...

  7. [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. 这道 ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [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 ...

随机推荐

  1. js页面内容只读,不可复制

    // document.oncontextmenu=new Function("event.returnValue=false"); // document.onselectsta ...

  2. STM32L15x——ADC采集DMA数据只第一次正确(已解决)

    前提:我用的芯片是STM32L系列,可能对其它STM32系列不完全适用,仅供参考! 一.问题描述 我在使用DMA方式读取单ADC单通道采集的数据时,发现只能正确的采集一次数据,后来的就一直与第一次的相 ...

  3. Git---初入开源代码管理库的学习过程003

    Git常用命令总结 上接<Git 初入开源代码管理库的学习过程>学了一周Git,基本有了个认识.每一位比我厉害的,都是大牛,网上找了几篇博客和教材(感谢你们),边学习边实践用了四天,写笔记 ...

  4. cas多方式登录相关知识点的总结

    知识点: cas多表单登录(在用户名,密码的基础上,增加短信验证码登录) 自定义认证策略 自定义字段添加为空校验的错误信息 Controller层接口的调用 一:场景 项目涉及到的业务是,在原cas用 ...

  5. svn 跟 git的区别

    1.svn是集中式版本控制系统,git是分布式版本控制系统 2.svn是直接与服务器进行交互,git是将文件存到本地然后再推送到服务器 3.svn必须在连网的情况下工作,git可以不连网开发 4.sv ...

  6. Luogu P2567 [SCOI2010]幸运数字 容斥+脑子

    双倍经验:BZOJ 2393 Cirno的完美算数教室 做法:先把$[1,r]$中所有的幸运数字筛出来,然后用这些幸运数字来筛$[l,r]$中的近似幸运号码: 剪枝:当一个幸运数字$a[i]$是另一个 ...

  7. Spring详解------事务管理

    目录 1.事务介绍 2.事务的四个特性(ACID) 3.Spring 事务管理的核心接口 4. PlatformTransactionManager  事务管理器 5.TransactionStatu ...

  8. JS 定时器-setInterval、clearInterval、setTimeout

    在微信小程序里写的: // pages/splash/splash.js const app = getApp() Page({ data: { remainSecond: }, // 页面渲染完成后 ...

  9. Crash的数字表格 / JZPTAB

    https://www.cnblogs.com/peng-ym/p/8666124.html #include<bits/stdc++.h> #define LL long long #d ...

  10. Zookeeper原理 一

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...