Java [Leetcode 96]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
解题思路:
动态规划法。
用G(n)表示长度为n组成的二叉搜索树的数目;
G(0) = 1, G(1) = 1
F(i,n)表示以i为根节点,长度为n组成的二叉搜索树的数目。
从而G(n) = F(1,n) + F(2,n) + ...+ F(n,n)
而F(i,n) = G(i - 1) * G(n - i) 1<=i <=n
从而G(n) = G(0) * G(n - 1) + G(1) * G(n - 2) + ... + G(n - 1) * G(0)
代码如下:
public class Solution{
public int numTrees(int n){
int[] res = new int[n + 1];
res[0] = res[1] = 1; for(int i = 2; i <= n; i++){
for(int j = 0; j <= i - 1; j++){
res[i] += res[j] * res[i - 1 - j];
}
} return res[n];
}
}
Java [Leetcode 96]Unique Binary Search Trees的更多相关文章
- [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(给定一个数字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? Example ...
- 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给定节点形成不同BST的个数
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Input: ...
- [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 ...
随机推荐
- HDU 3833 YY's new problem(换种思路的模拟,防超时)
题目链接 用p[a]保存的是输入的a在第p[a]个, 然后根据差值查找. #include<stdio.h> #include<string.h> int main() { ...
- 调用MYSQL存储过程实例
PHP调用MYSQL存储过程实例 http://blog.csdn.net/ewing333/article/details/5906887 http://www.cnblogs.com/kkchen ...
- UITbaleView上按钮的单选
设置Id属性,标记是哪个cell @property (nonatomic,assign)NSInteger Id; 设置一个普通状态和选中状态图片不同的按钮 _choose = [[UIButton ...
- POJ 1547
#include<iostream> #include<string> using namespace std; int main() { int length; int hi ...
- POJ 3468 A Simple Problem with Integers(线段树)
题目链接 题意 : 给你n个数,进行两种操作,第一种是将a到b上所有元素都加上c,第二种是查询a到b上所有元素之和输出. 思路 : 线段树,以前写过博客,但是现在在重刷,风格改变,,所以重新写一篇.. ...
- HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)
题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...
- WCF分布式开发步步为赢(9):WCF服务实例激活类型编程与开发
.Net Remoting的激活方式也有三种:SingleTon模式.SingleCall模式.客户端激活方式,WCF服务实例激活类型包括三种方式:单调服务(Call Service),会话服务(Se ...
- poj 1797(最短路变形)
题目链接:http://poj.org/problem?id=1797 思路:题目意思很简单,n个顶点,m条路,每条路上都有最大载重限制,问1->n最大载重量.其实就是一最短路的变形,定义wei ...
- Java 网络编程(一)
网络通讯要素 IP地址(InetAddress) 端口号 传输协议 图示: TDP/IP模型 以OSI 7层模型讲解数据的传输 InetAddress InetAddress类主要表示IP地址. ...
- Android异步下载图片并且缓存图片到本地
Android异步下载图片并且缓存图片到本地 在Android开发中我们经常有这样的需求,从服务器上下载xml或者JSON类型的数据,其中包括一些图片资源,本demo模拟了这个需求,从网络上加载XML ...