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
 
推导出递推公式即可:
对于1个元素来说,f[1]=1;
对于2个元素来说,有两种f[2]=2;
对于3个元素来说,
1) 把1作为根节点,则2,3都连在右边,共有2种
2)把2作为根节点,则1,2必须一个在左边,一个在右边,共1种
3)把3作为根节点,则2,3必须在左边,共有2种
 
把i作为根节点时,我们有前面i-1个元素放在左边,后面i+1到n个元素放在右边
 
 
所以对于f[n]我们有以下的公式:
 
f[n]=f[n-1]+f[1]f[n-2]+f[2]f[n-3]+......+f[n-2]f[1]+f[n-1];
 
引入f[0]=1,f[1]=1;
则f[n]=f[0]f[n-1]+f[1]f[n-2]+f[2]f[n-3]+......+f[n-2]f[1]+f[n-1]f[0];
 
 
 class Solution {
public:
int numTrees(int n) { if(n==||n==)
{
return ;
}
vector<int> f(n+); f[]=;
f[]=; for(int i=;i<=n;i++)
{
f[i]=;
for(int j=;j<i;j++)
{
f[i]+=f[j]*f[i-j-];
}
} return f[n];
}
};

【leetcode】Unique Binary Search Trees的更多相关文章

  1. 【leetcode】Unique Binary Search Trees II

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

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

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

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

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

  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. mysql的Replication机制

    mysql的Replication机制 参考文档:http://www.doc88.com/p-186638485596.html Mysql的 Replication 是一个异步的复制过程. 从上图 ...

  2. Python之路【第十四篇】前端补充回顾

    布局和事件 1.布局 首先看下下面的图片: 上面的内容都是居中的,怎么实现这个效果呢,第一种方法是通过float的方式,第二种是通过“div居中的方式” 第一种方式不在复述了,直接看第二种方式: 1. ...

  3. yii2 widget示例

    <?php namespace app\components; use yii\base\Widget; use yii\helpers\Html; class RctReplyWidget e ...

  4. MVC下分页的自定义分页一种实现

    1.引言 在MVC开发中我们经常会对数据进行分页的展示.通过分页我们可以从服务端获取指定的数据来进行展示.这样既节约了数据库查询的时间也节约了网络传输的数据量.在MVC开发中使用的比较多的应该是MVC ...

  5. InnerClass内部类

    1,内部类概述 定义:把A类定义在B类内部,则A类是内部类.如下所示: class Outer1{外部类 String name1; public void show(){ System.out.pr ...

  6. 【CISP笔记】操作系统安全

    账号安全设置 默认管理账号Administrator更名,设置密码(字母.数字.大小写字母.特殊字符,长度在8位以上). 本地安全策略 打开方式 win+R 输入ecpol.msc 账号锁定策略 用户 ...

  7. PHP 线程安全与非线程安全版本的区别深入解析

    Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍 ...

  8. QT中Sqlite的使用

    环境: 静态编译过sqlite 步骤: 1.C++链接器中加入Sqlite.lib,然后在测试一下是否能正常加载Sqlite驱动 #include<QtPlugin> Q_IMPORT_P ...

  9. Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析

    经过前面几篇文章的铺垫,今天我们终于要看看AQS的庐山真面目了,建议第一次看AbstractQueuedSynchronizer 类源码的朋友可以先看下我前面几篇文章: <Java并发包源码学习 ...

  10. STM32 之 NVIC(中断向量、优先级) 简述

    一.背景 需要使用STM32的CAN进行通信,经过一系列配置后,已可正常收发,还剩下一个CAN通信的错误处理.可错 误中断使能寄存器已经配置使能了,出错后就是无法进入"CAN1_SCE_IR ...