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

解析:

修改上例的顺序如下:

1        1        2        3        3

\        \      / \      /        /

3       2   1    3   2        1

/         \         /           \

2             3      1              2

可以看到,以 1 为根的树的个数,等于左子树的个数乘以右子树的个数,左子树是 0 个元素的树,右子树是 2 个元素的树。以 2 为根的树的个数,等于左子树的个数乘以右子树的个数,左子树是1个元素的树,右子树也是1个元素的树,以此类推。当数组为 1,2,3,...,n 时,基于以下原则的构建的BST树具有唯一性:以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1,n]构成。

定义f(i)为以[1,i] 能产生的 UniqueBinarySearchTree 的数目,则有

如果数组为空,毫无疑问,只有一种 BST,即空树,f(0) = 1。

如果数组仅有一个元素 1,只有一种 BST,单个节点,f(1) = 1。

如果数组有两个元素 1,2,那么有如下两种可能

1            2

\        /

2    1

f(2) =    f(0)∗f(1) ,1 为根的情况

+ f(1)∗f(0) ,2 为根的情况

再看一看 3 个元素的数组,可以发现 BST 的取值方式如下:

f(3) =   f(0)∗f(2) ,1 为根的情况

+ f(1)∗f(1) ,2 为根的情况

+ f(2)∗f(0) ,3 为根的情况

所以,由此观察,可以得出f的递推公式为

f(i) =∑f(k−1)×f(i−k)

因此,可以利用动态规划解决此问题:

/*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
*/
//#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <cstring> using namespace std;
class Solution {
public:
int numTrees(int n) {
vector<int>f(n+1,0);
f[0] = 1;
f[1] = 1;
for(int i=2; i<=n; i++){
for(int j=1; j<=i; j++){
f[i] +=f[j-1]*f[i-j];
}
}
return f[n];
}
}; int main(){
Solution s;
cout<<s.numTrees(3)<<endl;
system("pause");
return 0;
}

  

【leetcode】Unique Binary Search Trees (#96)的更多相关文章

  1. 【leetcode】Unique Binary Search Trees

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

  2. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

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

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

  4. 【leetcode】 Unique Binary Search Trees II (middle)☆

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

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

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

  6. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. 【Leetcode】【Medium】Unique Binary Search Trees

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

  8. 【Leetcode】【Medium】Unique Binary Search Trees II

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

  9. 【Leetcod】Unique Binary Search Trees II

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

随机推荐

  1. 使用Shell创建GitHub仓库

    Github的代码仓库分为2种类型: 用户自己的代码仓库 组织的代码仓库 下面就使用Shell脚本创建这2种类型的代码仓库,脚本如下 创建用户自己的代码仓库 #!/bin/bash USER_NAME ...

  2. 加载dll过程中assembly失败

    错误现象: 进行插件读取时出现错误:“尝试从一个网络位置加载程序集,在早期版本的 .NET Framework 中,这会导致对该程序集进行沙盒处理.此发行版的 .NET Framework 默认情况下 ...

  3. elk系列5之syslog的模块使用

    preface rsyslog是CentOs系统自带的的一个日志工具,那么我们就配置logstash来接受rsyslog的日志. logstash的syslog模块 linux-node2上操作 lo ...

  4. <<< 入侵网站类提权注入教程

    ---------------------------------------入侵类教程-------------------------------------------------------- ...

  5. 【转】JavaWeb MVC

    -------------------------------------------------------------------------------------------------- 1 ...

  6. tamtam-nuget-imageserver

    https://bitbucket.org/tamtam-nl/tamtam-nuget-imageserver/src/eaddb1ac943fcaa9e7ef210ed5a5ccf630b8699 ...

  7. oracle中时间处理

    --查看当前日期.时间SQL> select sysdate from dual; SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') ...

  8. [Python] 利用Django进行Web开发系列(一)

    1 写在前面 在没有接触互联网这个行业的时候,我就一直很好奇网站是怎么构建的.现在虽然从事互联网相关的工作,但是也一直没有接触过Web开发之类的东西,但是兴趣终归还是要有的,而且是需要自己动手去实践的 ...

  9. 【bzoj1725】[USACO2006 Nov]Corn Fields牧场的安排

    题目描述 Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土 ...

  10. 什么是Reactor模式,或者叫反应器模式

    Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应器模式,但更多好像就直接叫reactor模式了,其实我觉着叫应答者模式更好理解一些.通过了解,这个模式更像一个侍卫,一直在等待你的召唤,或 ...