题目:

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

解题:

用递归的思想,当仅仅有0个或是1个节点的时候。仅仅有一种。n个节点的时候有f(n)种:

左边能够有n-1个节点,右边0个节点,依据对称性能够左右互换。这时候有2*f(n-1)*f(0);

一边1个,还有一边n-2个,这时候有2*f(1)*f(n-2);

一边两个,一边N-3个,这时候有2*f(2)*f(n-3);

。。。

。。。

假设n为奇数,两边都是n/2个。这时候有f(n/2)*f(n/2),假设n为偶数,一边n/2一边(n/2+1)个,为2*f(n/2)*f(n/2+1);

代码:

  public static int numTrees(int n) {
if(n==1||n==0)
return 1;
int sum=0;
if(n%2==0)
{
for(int k=n-1;k>=n/2;k--)
{
sum+=2*numTrees(k)*numTrees(n-1-k);
}
return sum; }
else {
for(int k=n-1;k>n/2;k--)
{
sum+=2*numTrees(k)*numTrees(n-1-k);
}
return sum+numTrees(n/2)*numTrees(n/2);
} }

LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解的更多相关文章

  1. Leetcode Unique Binary Search Trees

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

  2. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  3. 【题解】【BST】【Leetcode】Unique Binary Search Trees

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

  4. leetcode 96 Unique Binary Search Trees ----- java

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

  5. 96. Unique Binary Search Trees

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

  6. Unique Binary Search Trees——LeetCode

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

  7. 【Leetcod】Unique Binary Search Trees II

    给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...

  8. LeetCode OJ 96. Unique Binary Search Trees

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

  9. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

随机推荐

  1. Spark2.2,IDEA,Maven开发环境搭建附测试

    前言: 停滞了一段时间,现在要沉下心来学习点东西,出点货了. 本文没有JavaJDK ScalaSDK和 IDEA的安装过程,网络上会有很多文章介绍这个内容,因此这里就不再赘述. 一.在IDEA上安装 ...

  2. 复习HTML+CSS(7)

    n  HTML 注释 <--! 注释内容 --> 注意:注释内容不会显示,注释是为了将来维护方面. n  图片热点(图像地图) 图像热点:给一张图片加多个链接,默认情况下,一张图片只能加一 ...

  3. OI知识点

  4. bootstrap 图片 图标

    一.图片 1.响应式图片:<img src="  " class="responsive"> 2.圆角图片:<img src="  ...

  5. H265

    H265 h265  一.名词 CTU: 编码树单元 CU: 编码单元 PU: 以CU为根,对CU进行划分,一个预测单元PU包含一个亮度预测块PB和两个色度预测块PB. TU: 以CU为根,变换单元T ...

  6. for 循环 乘法口诀表

    用for循环写乘法口诀表: for(var i = 1; i <= 9; i++) { var c=''; for(var x = 1; x <= i; x++) {    c=c+x+' ...

  7. 读书笔记「Python编程:从入门到实践」_11.测试函数

    11.1 测试函数 要学习测试,得有要测试的代码.下面是一个简单的函数,它接受名和姓并返回整洁的姓名: def get_formatted_name(first, last): "" ...

  8. redis与其可视化工具在win7上的安装

    步骤 1.下载安装Redis服务. 2.调用执行文件创建服务器以及测试缓存. 3.使用可视化工具redis-desktop-manager管理查询缓存. 1.下载安装Redis服务. 下载地址:htt ...

  9. Spring 团队开源 nohttp,尽可能不用 HTTP

    Spring 团队开源 nohttp 项目,用以查找.替换和阻止 http:// 的使用. 项目是为了在可能使用 https:// 的情况下不使用到 http://,确保不会发生中间人攻击.Sprin ...

  10. 三种“BIOS设置光驱第一启动的图解”

    除特殊机器类型一般都是“开机按DEL进入BIOS界面,当然还有AMD主板有按F2的进入” 第一种.AWARD BIOS设置光驱启动方法: 进入Bios界面,选第二项——>回车 选中此项,按PAG ...