lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
题目
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?
给出n = 3,有5种不同形态的二叉查找树:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
解题
public class Solution {
/**
* @paramn n: An integer
* @return: An integer
*/
public int numTrees(int n) {
// write your code here
int[] count = new int[n + 1];
if( n==0 )
return 1;
count[0] = 1;
count[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= i - 1; j++) {
count[i] = count[i] + count[j] * count[i - j - 1];
}
}
return count[n];
}
}
Java Code
lintcode 中等题:unique Binary Search Tree 不同的二叉查找树的更多相关文章
- Unique Binary Search Tree II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Lintcode: Remove Node in Binary Search Tree
iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...
- [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- Unique Binary Search Tree
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Unique Binary Search Tree - Leetcode
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Tree
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Leetcode 95. Unique Binary Search Tree II
由于BST的性质,所以右子树或者左子树中Node的值是连续的: 左子树 = [1, i -1], root = i, 右子树 = [i + 1, n].使用一个递归函数构造这个BST.其中返回值应该是 ...
随机推荐
- Atan2
在三角函数中,两个参数的函数atan2是正切函数的 一个变种.对于任意不同时等于0的实参数x和y,atan2(y,x)所表达的意思是坐标原点为起点,指向(x,y)的射线在坐标平面上与x轴正方向之间 的 ...
- LiteHttp:一款‘智能’的HTTP框架类库
LiteHttp:一款‘智能’的HTTP框架类库(转自github) 简介 LiteHttp是一款简单.智能.灵活的HTTP框架库,它在请求和响应层面做到了全自动构建和解析,主要用于Android快速 ...
- Maven入门指南⑤:使用Nexus搭建Maven私服
1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则,私服请求外部 ...
- ASP.NET Core 行军记 -----拔营启程
ASP.NET MVC 6:https://docs.asp.net/en/latest/mvc/index.html ASP.NET Core :https://docs.asp.net/en/la ...
- impdp ORA-29913: error in executing ODCIEXTTABLEOPEN callout
1.数据导出时的日志 ;;; Export: Release :: Copyright (c) , , Oracle and/or its affiliates. All rights reserve ...
- C#中的interface、virtual和abstract
一.Abstract: abstract方法必须在abstarct类中声明,没有默认实现,子类必须实现. 二.Virtual: virtual方法可以声明在abstract类中,也可以声明在非abst ...
- Java中的继承和多态
1. 什么是继承,继承的特点? 子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承方法,使得子类具有父类相同的行为. 特点:在继承关系中,父类更通用.子类更具体.父类具有更 ...
- Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们使用了Linq对Entity Framework进行了一个查询,但是通过学习我们却发现了懒加载给我来的性能上的 ...
- js SVG
Snap.svg Paths.js http://www.sitepoint.com/creating-animated-valentines-day-card-snap-svg/
- c#之委托总结
1.委托的声明和编译原理 声明委托: delegate void Translate(string str); 通过反编译之后可得下面代码 private sealed class Translate ...