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.其中返回值应该是 ...
随机推荐
- C++ const用法小结 (欢迎大家拍砖)
C++const 关键字小结 const 是constant的缩写,本意是不变的,不易改变的意思. const 在C++中是用来修饰内置类型变量,自定义对象,成员函数,返回值,函数参数. 一.cons ...
- 鼠标悬浮图片时弹出透明提示图层的jQuery特效
源码: <!doctype html> <html class="no-js" lang="en"> <head> < ...
- Messagebox.Show()常用参数设置
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(" 1 个参数 " ); } pr ...
- Debian7系统安装配置手册
一.安装系统 系统版本:Debian7 参考资料:http://www.myhack58.com/Article/48/66/2013/39802.htm 二.配置源 vi /etc/apt/sour ...
- 画了一张PHPCMSV9的运行流程思维导图
转载:http://www.cnblogs.com/fuyunbiyi/archive/2012/03/12/2391253.html
- How to force to Fullscreen Form
Is it possibile by code resize a form to fullscreen? (like button Maximize) ? // VAR Changed on 10 J ...
- Python-Day2 Python基础进阶之数据类型
一.数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) Python ...
- 管理口令(P):[INS-30001] ADMIN口令为空之Oracle安装
在安装oracle database11g 发行版的时候出现下面这个问题. 无论怎么输入密码都提示有问题,都输入得鬼火了!去百度了一下,果然有命名规则的 规则如下:小写字母+数字+大写字母
- android studio安装插件
1.File-Settings菜单
- oracle作业
http://blog.csdn.net/hao_ds/article/details/38382931 oracle作业各种参数的详细介绍