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

Input: 3
Output: 5
Explanation:
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

题意:

给定n个节点,可形成多少种不同的BST

思路:

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

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

如果数组有两个元素{1,2}, 那么有如下2种可能,                f(2)=2。

1             2
\ /
2 1

如果数组有三个元素{1,2,3}, 那么有如下5种可能,             f(3)=5。

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

由此得出规律,

对于任意以i为根节点的二叉树,

其左子树的值一定小于i,也就是[0, i - 1]区间,

而右子树的值一定大于i,也就是[i + 1, n]区间。

假设左子树有m种排列方式,而右子树有n种,则对于i为根节点的二叉树总的排列方式就是m x n

f(2) = f(0) * f(1) + f(1) * f(0);
f(3) = f(0) * f(2) + f(1) * f(1) + f(2) * f(0);
f(4) = f(0) * f(3) + f(1) * f(2) + f(2) * f(1) + f(3) * f(0);
....
f(n) = f(0) * f(n-1) + f(1) * f(n-2) + ... + f(n-2) * f(1) + f(n-1) * f(0); 【卡特兰数(Catalan)】

代码:

 class Solution {
public int numTrees(int n) {
if(n < 1) return 0;
int[] dp = new int[n+1];
dp[0] = 1;
dp[1] = 1;
for(int i = 2; i <= n; i++){
for(int j = 0; j < i; j++){
dp[i] += dp[j] * dp[i - j - 1];
}
}
return dp[n];
}
}




[leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数的更多相关文章

  1. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  2. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  3. 52. leetcode 96. Unique Binary Search Trees

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

  4. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

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

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

  6. Java [Leetcode 96]Unique Binary Search Trees

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

  7. leetcode 96 Unique Binary Search Trees ----- java

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

  8. [leetcode] 96 Unique Binary Search Trees (Medium)

    原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...

  9. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

随机推荐

  1. [R] t.test()

    t.test(x, y = NULL, alternative = c("two.sided", "less","greater"), mu ...

  2. PythonStudy——可变与不可变 Variable and immutable

    ls = [10, 20, 30] print(id(ls), ls) ls[0] = 100 print(id(ls), ls) print(id(10)) print(id(20)) print( ...

  3. 笔记本使用control线连接交换机

    要求: 1.一台笔记本 2.一条usb转rj45串口线 (一端是usb口一端是网口) 连接步骤: usb口插入笔记本,网口插入交换机控制口(交换机上面一般会有标注) 直连步骤: 首先查看是哪个com口 ...

  4. Feign中使用hystrix

    Feign中使用hystrix 一.在Order工程中的bootstrap.yml中增加配置 feign: hystrix: enabled: true

  5. AIUI开放平台:多轮对话返回前几轮语槽数据

    编写云函数: AIUI.create("v2", function(aiui, err){ // 获取 response response = aiui.getResponse() ...

  6. 黄聪:AngularJS最理想开发工具WebStorm

    Aug 29, 2013 Tags: angularangular.jsangularjswebstorm Comments: 23 Comments AngularJS最理想开发工具WebStorm ...

  7. 开机自启动 centos 7

    开机自启动,写入必要的命令即可.vim /etc/rc.d/rc.local

  8. 微信小程序精品demo

    http://www.jianshu.com/p/0ecf5aba79e1 感谢笔者的分享!

  9. UEditor (富文本编译器)

    下载网址:https://ueditor.baidu.com/website/download.html 开发文档:http://fex.baidu.com/ueditor/

  10. Calendar打印日历

    package com.example.demo; import org.junit.Test; import org.junit.runner.RunWith; import org.springf ...