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
BST树的定义:根节点左边所有节点的值小于根节点值,右边所有节点的值大于根节点的值,然后其左右子树又是BST

思路:例如序列:1,2,3,4,5

第一个数字的左边有0个数字,右边4个数字  dp[0]*dp[4]

第二个数字的左边有1个数字,右边3个数字  dp[1]*dp[3]

第三个数字的左边有2个数字,右边2个数字  dp[2]*dp[2]

第四个数字的左边有3个数字,右边1个数字  dp[3]*dp[1]

第五个数字的左边有4个数字,右边0个数字  dp[4]*dp[0]

所以我们要求的dp[5]就等于上面加起来的和,这里我们得假设dp[0]=1;

其实就是每个数字来当根节点,具体如下图所示


代码:
class Solution{
public:
int numTrees(int n) {
if(n<=) return n;
vector<int> dp(n+,); dp[]=;
for(int i=;i<=n;++i){
int temp=;
for (int j=;j<=i;++j)
{
temp+=dp[j-]*dp[i-j];
}
dp[i]=temp;
}
return dp[n];
}
};

Unique Binary Search Trees(dp)的更多相关文章

  1. LeetCode Unique Binary Search Trees (DP)

    题意: 一棵BST有n个节点,每个节点的key刚好为1-n.问此树有多少种不同形态? 思路: 提示是动态规划. 考虑一颗有n个节点的BST和有n-1个节点的BST.从n-1到n只是增加了一个点n,那么 ...

  2. 96. Unique Binary Search Trees(I 和 II)

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

  3. 96. Unique Binary Search Trees (Tree; DP)

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

  4. LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)

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

  5. 40.Unique Binary Search Trees(不同的二叉搜索树)

    Level:   Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...

  6. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  7. LeetCode(96) Unique Binary Search Trees

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

  8. 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

    [096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...

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

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

随机推荐

  1. JVM_Bind问题的解决方案

    心得:删除javaw.exe进程即可. 以下是网络的解决方案: JVM_Bind问题出现通常有两种情况. 一种是原来的javaw.exe没有结束掉而又新创建了一个javaw.exe进程.这本无可厚非, ...

  2. 前端什么是BFC

    什么是BFC? 全称块级格式化上下文?什么意思不懂.看了好多博客,基本都是抄的,真心都不是大白话.我今天来总结一下,用菜鸟级别的语言来描述. BFC 应该可以抽象成一个 独立的个体,出淤泥而不染的白莲 ...

  3. less算宽度 加~ width: calc(~"50% - 35px");

    less算宽度 加~  width: calc(~"50% - 35px");

  4. axios 两种异步模式,代理模式 和 异步模式

    axios 两种异步模式,代理模式 和 异步模式

  5. postman使用--发送请求

    概述 上节讲了下接口的基础,从现在来学习怎么测接口.当然,测试接口有很多的工具,比如postman,jmeter等等,或者用代码测试,如果是做接口自动化我当然会选python,如果是调试接口,我特别喜 ...

  6. laravel知识点备忘

    1.连表查询:select * from goods left join shop on goods.shopid=shop.shopid; DB::table('goods') ->leftJ ...

  7. Python-集合数据类型内置方法

    集合内置方法(必考) 用途:用于关系运算的集合体,由于集合内的元素无序且集合元素不可重复,因此集合可以去重,但是去重后的集合会打乱原来元素的顺序. 定义方式:{}内用逗号隔开多个元素,元素只能是不可变 ...

  8. Python中的类(2)

    一.使用类和实例 我们先编写一个学生的类,它存储了有关学生的信息,还有一个整合学生信息的方法: student.py class Student(): def __init__(self,name,a ...

  9. python_函数递归

    函数递归 函数递归:函数的递归调用,即在函数调用的过程中,又直接或间接地调用了函数本身 # import sys # print(sys.getrecursionlimit()) # sys.setr ...

  10. LeetCode(100) Same Tree

    题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...