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. Debian8搭建php环境

    安装apache 新装的系统发现 apt-get install apach<tab> 没有自动补全 请查看 这里 apt-get install apache2 安装mysql apt- ...

  2. nginx平滑升级

    1.查询原来安装配置信息 [root@t-scrmap1-v-szzb local]# netstat -unlatp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0: ...

  3. cocos2d-x打飞机实例总结

    写了一个cocos2d-x的打飞机游戏,为了深入了解,准备进入引擎内部,深入分析一下打飞机,顺便梳理一下相关的知识 打算分为几个部分: 1.程序入口和场景切换模块分析:简单了解HelloWorld怎样 ...

  4. fedora22切换用户windows分区不能自动挂载

    新建立一个用户后,然后登陆后,再次登出,登陆原来的账户windows分区不能自动挂载

  5. php cli配置文件问题

    引言 今天在教别人使用protobuf的时候,无意中发现了一个php cli模式下的诡异问题,费了老半天的找到解决方法了,这里拿出来分享下. 问题描述 我们这边最先引入了protobuf协议,使用的是 ...

  6. js中遍历删除数组中的项(项目中遇到的问题解决)

    代码如下: for (var key=0;key<$scope.pageContent.messages.length;key++){ if($scope.pageContent.message ...

  7. JavaScript 中一些概念理解 :clientX、clientY、offsetX、offsetY、screenX、screenY

    clientX 设置或获取鼠标指针位置相对于窗口客户区域的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条. clientY 设置或获取鼠标指针位置相对于窗口客户区域的 y 坐标,其中客户区域不包 ...

  8. LoadRunner 函数之 web_add_cookie

    简单示例: Action() { // 添加cookie web_add_cookie("is_login=True;path=/;domain=10.1.102.75"); // ...

  9. 2016-03-04记录 H264.TXT 转成 H264.h264

    H264.TXT文件 来源于板子上串口输出的数据,需要把该数据转成 *.h264用 H264的软件打开观察 txt中数据截图如下: MATLAB读入数据的代码: clc;close all;clear ...

  10. Bash 中 SHLVL 变量为 1000 的时候

    SHLVL 环境变量代表 Shell 嵌套执行的深度. $ echo $SHLVL 1 $ bash $ echo $SHLVL 2 $ bash $ echo $SHLVL 3 在 Bash 里,这 ...