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. Spring Data JPA引入和介绍

    第1章  1.ORM概述[了解] ORM(Object-Relational Mapping) 表示对象关系映射.在面向对象的软件开发中,通过ORM,就可以把对象映射到关系型数据库中.只要有一套程序能 ...

  2. 三、DQL语言

    目录 一.基础查询 (一)语法 (二)特点 (三)示例 二.条件查询 (一)语法 (二)筛选条件的分类 三.排序查询 (一)语法 (二)特点 四.常见函数 (一)介绍 (二)分类 五.单行函数 (一) ...

  3. C# 一个数组集合,任意组合,不遗漏,不重复

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  4. Django框架起步

    一.环境安装 二.创建项目 三.项目目录 四.创建项目应用 五.应用目录 六.第一个响应 七.第一个模板页面 八.第一个重定向 九.url应用移植 十.多应用相同模板页面冲突 十一.静态资源的配置 十 ...

  5. 关于Java的Object.clone()方法与对象的深浅拷贝

    文章同步更新在个人博客:关于Java的Object.clone()方法与对象的深浅拷贝 引言 在某些场景中,我们需要获取到一个对象的拷贝用于某些处理.这时候就可以用到Java中的Object.clon ...

  6. free命令详解-1

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存以及被内核使用的buffer.我们本篇学习如何使用free命令监控系统的内存情况. 一般使用free –m方式查看内存占用情况( ...

  7. SpringBoot + Maven + Hibernate ( 简单实现CRUD功能 )

    工具:idea.mariadb数据库 创建一个项目 ( student ) ........(使用idea创建一个springboot项目,这里我就不多说了) Maven 中的依赖 <?xml ...

  8. 操作mysql 中文乱码情况

    解决方法  : 在连接字符串中设置charset=utf8  即可正常添加中文字符 <add name="mtgzghEntities" connectionString=& ...

  9. HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 4

    /* HDU 6073 - Matching In Multiplication [ 图论 ] | 2017 Multi-University Training Contest 4 题意: 定义一张二 ...

  10. HDU 6061 - RXD and functions | 2017 Multi-University Training Contest 3

    每次NTT都忘记初始化,真的是写一个小时,Debug两个小时- - /* HDU 6061 - RXD and functions [ NTT ] | 2017 Multi-University Tr ...