[LeetCode_96] Unique Binary Search Trees
题目链接
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的更多相关文章
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 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 ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
随机推荐
- git异常
1. SSL certificate problem: self signed certificate 因git默认是ssl方式验证,在采用http请求时,是使用的账号密码方式,因此需要git放行. ...
- NativeClient开发指南
https://blog.csdn.net/column/details/24458.html
- 尚硅谷springboot学习2-微服务
2014年,martin flowler发表关于微服务的博客 微服务是一种架构风格:一个应用应该是一组小型服务:可以通过HTTP的方式进行互通: 单体应用:ALL IN ONE 微服务:每一个功能元素 ...
- 工厂模式——Head First
这里主要谈论其中的工厂方法模式和抽象工厂模式. 一.定义 工厂方法模式(Factory Method Pattern)定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推 ...
- 【x】 PAT/BasicLevel_C++/1002. 写出这个数 (20).cpp
C++中的to_string()函数[C++11支持] - Bravo Yeung-羊较瘦之自留地 - CSDN博客https://blog.csdn.net/lzuacm/article/detai ...
- Letter S Pronounced [z]
Letter S Pronounced [z] Share Tweet Share Since English is not a phonetic language, one letter is no ...
- ReactiveX 学习笔记(19)使用 RxSwift + RxCocoa 进行 GUI 编程
课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...
- Linux IP和网关配置
操作环境 SuSE11/SuSE10 配置方法一<永久有效,重启不失效> 通过修改/etc/sysconfig/network/ifcfg-eth*文件直接配置,服务器重启不失效,建议使用 ...
- python第三步骤(pygame)
1:先安装homebrew(类似于yum /apt-get为什么需要它呢,因为pip安装的时候需要很多的包的依赖,sdl什么的), 2:pip 安装pygame 我讨厌的环境变量问题 然后 通过的是 ...
- C# HttpWebRequest 模拟下载
C# web 获取服务端cookie 原文地址:https://www.cnblogs.com/louby/p/5569536.html C#多线程环境下调用 HttpWebRequest 并发连接限 ...