[leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数
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的个数的更多相关文章
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- 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]* ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- Java [Leetcode 96]Unique Binary Search Trees
题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- 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 ...
- [leetcode] 96 Unique Binary Search Trees (Medium)
原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...
- [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 ...
随机推荐
- 使用parted对大于2T的磁盘进行分区
使用parted对磁盘进行分区 版本信息 版本 修改日期 修改人 修改内容 备注 V0.1 2018/09/06 初始化版本 讨论稿 ...
- zombodb 聚合函数
zombodb 暴露基本上所有es 的集合函数为sql 函数,我们可以方便使用 比如 count FUNCTION zdb.count( index regclass, query zdbquery) ...
- shell(2)图片重命名
1:图片重命名 原来的图片名字格式: 改成的图片名字格式: #!/bin/bash #重命名 .png和.jpg #如果原文件的图片名称是从0开始,那么count=:从1开始,那么count= cou ...
- nodeppt:网页版 PPT
资料 网址 github https://github.com/ksky521/nodeppt 网页版PPT(nodeppt 的介绍) http://deliazhi.com/2017/03/31/W ...
- django模板语言循环字典,及forloop
views: from django.shortcuts import render,redirect from django.shortcuts import HttpResponse # Crea ...
- Python字符串列表元祖字典的公共方法
运算符 运算符 Python 表达式 结果 描述 支持的数据类型 + [1, 2] + [3, 4] [1, 2, 3, 4] 合并 字符串.列表.元组 * 'Hi!' * 4 ['Hi!', 'Hi ...
- 【常用命令】Linux相关命令
[[TOC]] iostat - 查看系统I/O状况 -k Display statistics in kilobytes per second -m Display statistics in me ...
- FreeBsd网络性能优化方案sysctl
以下是阿盛的配置 sysctl net.inet.tcp.msl= sysctl net.inet.tcp.mssdflt= sysctl net.inet.tcp.minmss= sysctl ne ...
- python之路——7
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1. 小数据池 int -5---256 str 特殊字符 *202. ASCII码 8位 1字节 - ...
- ubuntu18.04 安装mysql server
mysql 5.7支持的最高版本是Ubuntu17 ,即使安装成功后,也会出现各种妖蛾子,本人就被这种问题困扰了好一会.在Ubuntu 18.04下安装mysql,建议安装8.0以上版本! 1. 配置 ...