【树】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 ...
随机推荐
- trcd_extract_EDCD_new
# -*- coding:utf-8 -*- import re ''' 适应新版本 ''' year='17A'#用户自定义 ss='./data/'#根目录 filename = ss+'EDCD ...
- C语言中union关键字
union 关键字的用法与struct 的用法非常类似. union 维护足够的空间来置放多个数据成员中的“一种”,而不是为每一个数据成员配置空间,在union 中所有的数据成员共用一个空间,同一时间 ...
- line tension
<!DOCTYPE html> <html> <head> <title>tension</title> <script type=& ...
- JSTL自定义函数完成ACL即时认证
即时认证是指,用户进行查询或更新操作时,判断该用户进行是否对该操作有权限. 这里以判断用户是否有删除权限为例.如果用户有删除权限,即显示该按钮:如果没有删除权限,则不显示该按钮. 1.Manager层 ...
- 『IOS』 遇到问题记录(长期更新)
遇到的很多问题,解决后都是自己记着,以为不会忘记,之后却会想不起来了. 所以把今后解决的问题记录在这. 一. 在二级页面设置了CAlayer的代理,在返回一级页面报错: EXC_BAD_ACCESS( ...
- ECG心电图数据2
1.如何下载获取MIT-BIH的数据从下面这个官方链接页面可以下载到所有48组MIT-BIH心电数据: http://www.physionet.org/physiobank/database/mit ...
- 一般处理程序获取Layui上传的图片
asp.net利用一般处理程序获取用户上传的图片,上传图片利用的layui 前台页面 <%@ Page Language="C#" AutoEventWireup=" ...
- SQL处理数据并发,解决ID自增
1 创建MaxIdProcess表,由于存储ID的最大值 CREATE TABLE [dbo].[MaxIdProcess]( ,) NOT NULL, --自增ID ) NOT NULL, --存储 ...
- 我的第一个网络爬虫 C#版 福利 程序员专车
最近在自觉python,看到了知乎上一篇文章(https://www.zhihu.com/question/20799742),在福利网上爬视频... 由是我就开始跟着做了,但答主给的例子是基于pyt ...
- cpu缓存java性能问题初探
在内存与cpu寄存器之间,还有一块区域叫做cpu高速缓存,即我们常常说的cache. cache分为L1.L2.L3三级缓存,速度递减,离cpu越来越远,L1.L2每个内核自己都有,L3是每个插槽上的 ...