Unique Binary Search Trees
2015-07-29 21:09
507 查看
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.
https://leetcode.com/discuss/24282/dp-solution-in-6-lines-with-explanation-f-i-n-g-i-1-g-n-i https://github.com/zxysnow/leetcode/commit/7cf306e7b9eb8841371f926a5ac51b524c96138a
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
public class Solution { public int numTrees(int n) { int[] G = new int[n+1]; G[0] = G[1] = 1; for(int i = 2; i <= n; i++){ for( int j = 1; j <= i; j++){ G[i] += G[j-1]*G[i-j]; } } return G ; } }
https://leetcode.com/discuss/24282/dp-solution-in-6-lines-with-explanation-f-i-n-g-i-1-g-n-i https://github.com/zxysnow/leetcode/commit/7cf306e7b9eb8841371f926a5ac51b524c96138a
相关文章推荐
- HDU 4908 BestCoder Sequence
- UI01_UIView
- UITableView中Cell的位置交换
- 60. Permutation Sequence
- UIView的layoutSubviews和drawRect方法何时调用
- 经典中的经典Unique Binary Search Trees II
- hdoj-1159-Common Subsequence【动态规划求最长公共子序列】
- 利用UIView做出霓虹灯的效果
- 求逆序对——Ultra-QuickSort 题解解析
- UIView
- IOS开发之UITableView1
- Snail—UI学习之系统标签栏UITabBarController
- POJ 1679 The Unique MST
- ios UI 适配布局相关文章
- CodeForces 5C Longest Regular Bracket Sequence
- Beginners Guide To Learn Dimension Reduction Techniques
- UI02_Label
- ExQuilla的使用和破解
- StringBuffer 与 StringBuilder类的使用
- Android API Guides 阅读笔记(7)----Overview Screen