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. jQuery EasyUI 数据网格

    jQuery EasyUI 数据网格 - 转换 HTML 表格为数据网格 本节将介绍jQuery EasyUI数据网格的运用,主要内容为如何将HTML表格转换为数据网格. 本实例演示如何转换表格(ta ...

  2. OSI网络通信工作流程的标准化 ----- 理论

    OSI 七层模型 1 应用层 [提供用户服务,具体功能由特定的程序而定] 2 表示层 [数据的压缩优化,加密] 3 会话层 [建立应用级的连接,选择传输服务] 4 传输层 [提供不同的传输服务,流量控 ...

  3. Vim技巧----选取一个单词

    viw 它的作用是选取一个单词(word),无论光标在这个单词的哪个位置都能选中整个单词. 每日一Vim(18)Text-Object 前两节讲了Visual mode相关内容,这里提一个小问题,“如 ...

  4. vim文本编辑及文件查找应用3

    文件查找 locate,find两个命令 在文件系统上查找符合条件的文件: 实现工具:locate,find locate命令: 依赖于事先构建好的索引库,索引库可以由下边两种方式构建 系统自动实现( ...

  5. nodejs建站+github page 建站问题总结

    本文介绍 昨天吃晚饭的时候,在B站偶然看到一个关于搭建自己博客的视频,过程讲的很详细,于是就有了自己想尝试一下的冲动,所以,在晚上的时候,尝试了下,但是,过程并没有视频中说的那么顺利,看了网上很多帖子 ...

  6. Ping-Pong (Easy Version)的解析

    原题链接:http://codeforces.com/problemset/problem/320/B 之前自己做的时候一直读不懂题意,看了大佬的博客才知道是用dfs写,一道暴力搜索大水题https: ...

  7. C++踩坑记录(一)std:;string的析构

    之前写服务端程序有一个往消息队列里面推json的过程,然后发现推进去C#端取到的无论如何都是个空指针 简单复现一下现场 string str1 = string("hello1") ...

  8. 转:SpringBoot 自定义异常@ContollerAdvice ExceptionHandler不起作用

    原文链接:https://blog.csdn.net/evanxuhe/article/details/78650979 为了统一异常,我们通常定义一个统一管理所有Exception,包括自定义Exc ...

  9. duilib学习领悟(1)

    学习duilib已经有一段时间,一直没时间写总结,今天得出空来,写写心得体会! 由于本人知识有限,若有错误地方,望批评指正.多谢.! 初识duilib 刚开始接触duilib的时候,觉的他好神奇,整个 ...

  10. Load store and memoryless

    metal https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/MTLBestPractice ...