Unique Binary Search Trees:求生成二叉排序树的个数。

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

算法分析:类似上阶梯,简单的动态规划问题。当根节点为i时,比i小的节点有i-1个,比i大的节点有n-i个,所以,i为根节点能够生成二叉排序树的个数是

nums[n] += nums[i-1]*nums[n-i],i从1到n。

public class UniqueBinarySearchTrees
{
public int numTrees(int n)
{
if(n <= 0)
{
return 0;
}
int[] res = new int[n+1];
res[0] = 1;
res[1] = 1;
for(int i = 2; i <= n; i ++)
{
for(int j = 1; j <= i; j ++)//j为根节点
{
res[i] += res[j-1]*res[i-j];
}
}
return res[n];
}
}

Unique Binary Search Trees2:求生成二叉排序树的根节点的集合

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

For example,
Given n = 3, your program should return all 5 unique BST's shown below.

   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

算法分析:这个不是求个数,而是求生成树根节点。使用递归。

public class UniqueBinarySearchTreesII
{
public List<TreeNode> generateTrees(int n)
{
if(n <= 0)
{
return new ArrayList<TreeNode>();
} return helper(1, n);
} public List<TreeNode> helper(int m, int n)
{
List<TreeNode> res = new ArrayList<>();
if(m > n)
{
res.add(null);
return res;
} for(int i = m; i <= n; i ++)
{
//i为根节点
List<TreeNode> ls = helper(m, i-1);//i节点的左子树
List<TreeNode> rs = helper(i+1, n);//i节点的右子树
for(TreeNode l : ls)
{
for(TreeNode r : rs)
{
TreeNode curr = new TreeNode(i);
curr.left = l;
curr.right = r;
res.add(curr);
}
}
}
return res;
}
}

Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树的更多相关文章

  1. 41. Unique Binary Search Trees && Unique Binary Search Trees II

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

  2. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

  3. Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II

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

  4. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  5. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

  6. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

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

  7. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  8. 2 Unique Binary Search Trees II_Leetcode

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

  9. 【leetcode】Unique Binary Search Trees (#96)

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

随机推荐

  1. 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活--hdu2191(多重背包模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191 标准的多重背包 题目 有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是 ...

  2. Spring基本功能-扫描与继承

    一.Spring的扫描 一个稍大的项目中,可能会有成百上千个bean,此时采用xml的配置形式注入bean,一方面是配置文件显得十分庞大,另一方面也会导致后期的维护难度增加,为 此,Spring引入了 ...

  3. SCADA 必备函数之 :关于消息的函数

    Message Functions BroadcastSystemMessage//是将一条系统消息广播给系统中所有的顶级窗口. BroadcastSystemMessageEx//将消息发送到指定的 ...

  4. mariadb多源复制 muiltil source replication

    环境:centos-6.5      Mariadb:10.1.13-MariaDB 多源复制:企业数据库中写的需求较大,以至于I/O负载比较大,那么就必须把写的操作分摊到多台主服务器上进行,然后在将 ...

  5. git 更新某个文件

    1.拉取某个仓库的某个文件 git fetch git checkout origin/master test.php

  6. bin2lib shell脚本

    #!/bin/sh#输入文件名filename=$1#分割文件大小filesize=4096#输出库文件名libname="lib"$(echo $filename | tr . ...

  7. cocos2dx lua invalid 'cobj' in function 'lua_cocos2dx‘ 躺坑

    for 循环中保存了创建的 Node节点到 userdata 数据结构中 再次引用发现 一直报 LUA ERROR: [string ".\framework/cocos2dx/NodeEx ...

  8. ImageNet历年冠军和相关CNN模型

    ImageNet 是一个超过15 million的图像数据集,大约有22,000类. 是由李飞飞团队从2007年开始,耗费大量人力,通过各种方式(网络抓取,人工标注,亚马逊众包平台)收集制作而成,它作 ...

  9. I.MX6中PC连接开发板问题

    修改板端的文件 添加登录密码: passwd  vi /etc/network/interrfaces 在auto eth0下增加auto eth1 如果采用固定ip方式可以在后面增加一段固定ip设置 ...

  10. Python面试题之多个装饰器执行顺序

    疑问 大部分涉及多个装饰器装饰的函数调用顺序时都会说明它们是自上而下的,比如下面这个例子: def decorator_a(func): print 'Get in decorator_a' def ...