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.其中返回值应该是 ...
随机推荐
- memcach 安装
Windows7 x64在Wampserver上安装memcache 2012-07-13 0个评论 收藏 我要投稿 Windows7 x64在Wampserver上安装m ...
- file与 byte[] 互转
byte 转file String filepath="D:\\"+getName(); File file=new File(filepath); ...
- CSS3制作立体导航
<ul class="nav"> <li><a href="">首页</a></li> <li ...
- 近期H5项目开发小结
前言:2016差不多又过了半啦,最近参与了公司好几个h5项目(严格来说,也只能算是推广页面活动).主要是新品牌的推广需要,当然也有给公司以前老客户做的案例.今天主要总结下为新品牌开发的2个h5推广:就 ...
- struts2 type="redirect"源码解析
首先解释一下几个名词: request.getRequestDispatcher()是请求转发,前后页面共享一个request ; response.sendRedirect()是重新定向,前后页面不 ...
- [大牛翻译系列]Hadoop 翻译文章索引
原书章节 原书章节题目 翻译文章序号 翻译文章题目 链接 4.1 Joining Hadoop(1) MapReduce 连接:重分区连接(Repartition join) http://www.c ...
- Crusher Django 学习笔记1 hello world
http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial1-hello-world.html 随便学习一下goagent ...
- 解决前端浏览器传JSON对像到服务端后全部变成string类型的方法
这几天公司用nodejs+mongodb来做些东西,UI用的是kendo UI 碰到一个问题: 举个例子var a={"name":"张三","age ...
- WPF之UseLayoutRounding和SnapsToDevicePixels
最近在工作中看别的朋友XML代码时,发现SnapsToDevicePixels 属性然后通过查询资料了解其作用 1)UserLayoutRounding为False,导致控件布局相对屏幕若不是整数则不 ...
- python之方法总结
python的OOP的方法有3种: 1. 实例方法: 接收self参数 2. 类方法: 接收cls参数, 并要用classmethod()注册或者@classmethod注解. 3. 静态方法: 不接 ...