题目链接

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. Linux服务器mysql,nginx等自动停止的排查,以及解决方法

    最近发现自己使用的某台云服务武器只要稍微流量大一点,就会出现莫名的的数据库连接错误的情况 ,然后连上服务器查看,发现mysql进程被结束了,经常出现这种情况,这还怎么得了,然后第一时间查看mysql日 ...

  2. mysql 日期时间运算函数

    时期时间函数 DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-0 ...

  3. xlwt模块

    python使用xlwt模块操作Excel 该模块安装很简单 $ pip install xlwt 语法 import xlwt # 创建一个workbook 设置编码 workbook = xlwt ...

  4. Linux系统基础5周入门精讲(服务器介绍)

    使用工具:(1)源码管理(git工具):(2)自动部署:(3)web服务器 linux基础 服务器介绍 运维的职责:运行和维护服务器 1数据不能丢----大片不能没 2保证网站7*24小时允许---( ...

  5. django组件:中间件

    全局性的逻辑处理 一.中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨 ...

  6. js 正则函数初级之二

    1. 小括号在正则中: 1.1 小括号:表示分组 1.2 分组之后,,每个组都有一个序号,从左到右,依次为1,2,3.......:可以使用 RegExp.$1,RegExp.$2,RegExp.$3 ...

  7. C#项目单步调试莫名结束问题

    今天在调试一个问题时,单步跟踪,走到某一步时突然跳出了调试,后面很多断点一个都不进来. 经过更细致的一步步调试(进入每个函数查看),定位到如下一段代码有问题: 原因是:size = 3,buff_id ...

  8. LeetCode OJ 19. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. 第五次Scrum冲刺

    第五次Scrum冲刺 1.成员今日完成情况 队员 今日完成任务 刘佳 前端初步构建 李佳 后端设计初级阶段 周世元 数据设计 杨小妮 博客编写 许燕婷 管理团队当日及次日任务 陈水莲 测试矩阵用例设计 ...

  10. 使用__future__实现从python2.7到python3.x的过渡

    参考链接:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820023 ...