题目

给出 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 不同的二叉查找树的更多相关文章

  1. Unique Binary Search Tree II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  2. 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. ...

  3. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

  4. 【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 ...

  5. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  6. Unique Binary Search Tree

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. Unique Binary Search Tree - Leetcode

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. [LeetCode] Unique Binary Search Tree

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. Leetcode 95. Unique Binary Search Tree II

    由于BST的性质,所以右子树或者左子树中Node的值是连续的: 左子树 = [1, i -1], root = i, 右子树 = [i + 1, n].使用一个递归函数构造这个BST.其中返回值应该是 ...

随机推荐

  1. Linux进程间通信方法总结

    ①匿名管道(pipe) 匿名管道(pipe)管道是一种半双工的通信方式,数据只能单向流动.如果要进行双工通信,需要建立两个管道.管道只能在具有亲缘关系的进程间使用,例如父子进程或兄弟进程. ②有名管道 ...

  2. 初步了解SequoiaDB数据库

    随着企业中日益复杂与多变的需求,以及迅速扩展带来的海量数据的业务,IT部门需要将越来越多的信息提供给用户,同时在现今的全球经济背景环境下,IT部 门还需要在提供高效服务的同时,降低其设备与程序维护成本 ...

  3. 如何去掉html中的超链接

    $a= preg_replace("/<a[^>]+>/", "", $a); $a= preg_replace("/<\/a ...

  4. 使用GDataXML解析XML文档

    转载自:http://blog.csdn.net/tangren03/article/details/7868246 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是 ...

  5. IOC学习

    控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...

  6. Python开发【第一篇】Python基础之函数递归

    函数递归 递归的本质: 就是一个函数调用另外一个函数. def d(): return '123' def c(): r = d() return r def b(): r = c() return ...

  7. 括弧匹配检验(check)

    /*题目:括弧匹配检验 检验给定表达式中括弧是否正确匹配 (两种括弧“( ) ”“[]" ,正确输出OK,错误则输出wrong. 2016年8月8日07:24:58 作者:冰樱梦 */ # ...

  8. centos Supervisor

    Supervisor是一个进程监控程序. 满足的需求是:我现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...

  9. Export Farm Solution wsp Files SharePoint 2007 and 2010

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")$farm = [Microsof ...

  10. Windows 10 响应式设计和设备友好的开发

    使用Effective pixels有效像素设计UI 什么是缩放像素和Effective有效像素: 当你的应用程序运行在Windows的设备,系统用一个算法控制的规范,字体,和其他UI元素显示在屏幕上 ...