64-Unique Binary Search Trees
Submissions: 220611 Difficulty: Medium
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
思路: 1 2 3 4 5 | 6 | 7 ..........n
既然是二叉搜索树,先递增排好序,然后在序列中选定一个根,比如上图的6
然后分为左子树和右子树,这样便可以转化为子问题
设f(n)为n个数的二叉排序树的数目
则f(n) = f(0)f(n-1)+f(1)f(n-2)+.......+f(n-1)f(0)
自底向上写出公式即可
时间复杂度O(n*n),空间O(1)
效率更高可考虑直接用卡特兰公式
class Solution {
public:
int numTrees(int n) {
vector<int> f(n+1);
f[0]=1;
for(int i=1;i<=n;++i)
{
for(int k=0;k<=i-1;++k)
f[i]+=f[k]*f[i-1-k];
}
return f[n];
}
};
64-Unique Binary Search Trees的更多相关文章
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- [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 ...
随机推荐
- 【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)
简介 学习Redis Cluster的第一步,即本地搭建Redis Cluster.但是在Redis的官方文档中,是介绍在Linux系统中搭建Redis Cluster.本文主要介绍在Windows系 ...
- VMware Workstation 16.2 Pro for Linux SLIC 2.6 & Unlocker
请访问原文链接:https://sysin.org/blog/vmware-workstation-16-linux-slic/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin. ...
- JavaScript中的this对象指向理解
在JavaScript中,this不是固定不变的,它的指向取决于上下文环境,一般的,认为this指向使用它时所在的对象.主要有以下几类指向: 在方法中,this 表示该方法所属的对象. 如果单独使用, ...
- Linux 内核网桥源码分析
Linux网桥源码的实现 转自: Linux二层网络协议 Linux网桥源码的实现 1.调用 在src/net/core/dev.c的软中断函数static void net_rx_action(st ...
- JAVA笔记7__接口应用/Object类/简单工厂模式/静态代理模式/适配器模式
/** * 接口应用 */ public class Main { public static void main(String[] args) { Person p = new Person(&qu ...
- sed 替换命令使用
输入文件不会被修改,sed 只在模式空间中执行替换命令,然后输出模式空间的内容.文本文件 employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manage ...
- telnet IP 端口 的作用
测试远程服务器的端口是否开启
- 解决虚拟机linux系统全屏问题
修改设置 1) 如下图右单击虚拟机名,选择[settings-],调出虚拟机设置界面. 2) 在设置界面选择[hardware]->[CD/DVD2(IDE)]->[Connection] ...
- 2019年java大型项目技术选型
学习一下 1,公司使用的架构是:SpringCloud + K8S 这一套主流技术,但是还是入门级别的. 还包含apollo ,xxlJob ,SkyWalking,Cat,GrayLog等 2,G ...
- docker容器运行java后台程序,存到数据库的时间差一天的问题
主要原因是docker容器中的时间用的是标准时间,不是用的宿主机的时间. 修改方法: docker run -e TZ="Asia/Shanghai" -d -p 80:80 -- ...