【树】Unique Binary Search Trees
题目:
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
思路:
依次把每个节点作为根节点,左边节点作为左子树,右边节点作为右子树,那么总的数目等于左子树数目*右子树数目
/**
* @param {number} n
* @return {number}
*/
var numTrees = function(n) {
if(n==0){
return 1;
}
if(n==1){
return 1;
} var count=[];
var temp;
count[0]=1;
count[1]=1;
count[2]=2;
for(var i=3;i<=n;i++){
temp=0;
for(var k=0;k<i;k++){
temp+=count[k]*count[i-k-1]
}
count[i]=temp;
}
return count[n]; };
【树】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】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 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 ...
- Unique Binary Search Trees I & II
Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...
- LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- Unique Binary Search Trees II leetcode java
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
随机推荐
- python文件对比
#-*- encoding:utf-8 -*- class loadDatas(object): def __init__(self): self.path='./data' def load_com ...
- MFC框架仿真<二>
- (KMP 暴力)Corporate Identity -- hdu -- 2328
http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...
- hdu 4983 欧拉函数
http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...
- bootstrap modal
模态框提供了两个可选尺寸,通过为 .modal-dialog 增加一个样式调整类实现.加modal-lg,加modal-sm,不加也可以,共有三种尺寸. 触发方式,data-target, 感觉比js ...
- jpg/png格式图片转eps格式的方法--latex自带命令bmeps
bmeps -h 命令查看bmeps的帮助信息 bmeps -c example.jpg example.eps
- Android 体系架构
什么是Android? 答:Android就是移动设备的软件栈,包括(一个完整的操作系统,中间件,关键应用程序), 底层是Linux内核,包括(安全管理, 内存管理,进程管理 ,电源管理,硬件驱动-) ...
- 《Are your lights on?》读后感
楔子(看过某类小说的孩纸对此应该不陌生...): <你的灯亮着吗?>讲了些什么?它为我们总结了解决问题的一般方法?不,它只是建议我们遇到问题后应该怎么做(绝对不等于解决问题的方法).这些建 ...
- mySql数据库 C#使用guid
CHAR(36) 如果某列设置为CHAR(36),则MySQL官方的连接器会将其当成 GUID 类型.实际上,有时候 某个字段碰巧设为可CHAR(36), 但是我们的本意并非当它是GUID. varc ...
- OSLab课堂作业2
日期:2019/3/23 内容: 实现内容 要求 mysys.c 实现函数mysys,用于执行一个系统命令. mysys的功能与系统函数system相同,要求用进程管理相关系统调用自己实现一遍 使 ...