题目链接

https://leetcode.com/problems/unique-binary-search-trees/

题意

计算给定节点数的BST有多少种

思路

递归

相关知识

二叉搜索树(Binary Search Tree ,BST)

A BST is defined as follows:

1 The left subtree of a node contains only nodes with keys less than the node's key.

2 The right subtree of a node contains only nodes with keys greater than the node's key.

3 Both the left and right subtrees must also be binary search trees.

特点:

1.每个节点的值大于其任意左侧子节点的值,小于其任意右节点的值。

2.平衡时性能逼近二分查找,但相比连续内存空间的二分查找,插入删除操作开销更小,但多次插入删除可能造成树的不平衡,最坏接近线性查找。

代码

class Solution {
public:
int numTrees(int n) {
if(n==0||n==1){return 1;}
else{
int BSTCnt=0;
for(int i=1;i<=n;i++){
BSTCnt+=numTrees(i-1)*numTrees(n-i);
}
return BSTCnt;
}
}
};

reference

https://www.cnblogs.com/Leo_wl/p/5229585.html

[LeetCode_96] Unique Binary Search Trees的更多相关文章

  1. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  4. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  5. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  7. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  8. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  9. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

随机推荐

  1. jeecg好用吗,看看大家的评价

    大家都会有个疑问,jeecg好用吗? 看看大家的评价

  2. 爬虫--Scrapy-持久化存储操作2

    1.管道的高级操作 将爬取到的数据值分别存储到本地磁盘.redis数据库.mysql数据. 需求:将爬取到的数据值分别存储到本地磁盘.redis数据库.mysql数据. 1.需要在管道文件中编写对应平 ...

  3. 尚硅谷springboot学习22-Thymeleaf入门

    Thymeleaf是一种模板引擎,类似于JSP.Velocity.Freemarker

  4. eclipse打断点,进行弹窗提示后点击是才进入debug视图,这个要怎么恢复

    window --> preferences --> Run/Debug --> Perspectives 里的 open the associated perspective wh ...

  5. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  6. Excel导入

    public static Collection importExcelByIs(InputStream inputstream, List<CgFormFieldEntity> list ...

  7. angularjs 做不到实时脏值查询

    angularjs 做不到脏值查询 ,数据请求过来,不操作其他按钮,请求的值就是展示不出来:(相当于,只有手动触发,angularjs内部才会把脏值查询出来): 解决办法:在请求过来的值旁边加上$sc ...

  8. 为tomcat配置项目必须的引擎文件

    1.如下图所示,红框圈出来的四个语音引擎文件是直接放在项目根目录下的,这样的话我们发布web应用的时候,项目并不能自动把这几个文件打包到tomcat中, 除非放到WebRoot文件夹下,但是这样的话项 ...

  9. Hibernate学习笔记1.1(简单插入数据)

    Hibernate是把以前的jdbc连接数据库的操作进行了一系列友好的封装,最好只用调用save即可,即将sql语句的这部分操作转化为面向对象的 Hibernate资源准备: 文档目录结构: 1.网址 ...

  10. 通过日志恢复SQL Server的历史数据

    通过日志恢复SQL Server的历史数据 Posted on 2008-11-14 21:47 代码乱了 阅读(4658) 评论(10)  编辑 收藏 园子里前段时间发过一篇通过日志恢复MSSQL数 ...