*[Lintcode]Unique Binary Search Trees 不同的二叉查找树
2016-11-17 16:42
459 查看
Given n,
how many structurally unique BSTs
(binary search trees) that store values 1...n?
Example
Given n = 3, there are a total of 5 unique BST's.
分析:递归法。n=19时超时.
public class Solution {
/**
* @paramn n: An integer
* @return: An integer
*/
public int numTrees(int n) {
if(n == 0 || n == 1) return 1;
return helper(n, 1, n);
}
int helper(int n, int start, int end) {
if(start == end) return 1;
int res = 0;
for(int i = start; i <= end; i++) {
int left = 1, right = 1;
if(i > start) left = helper(n, start, i - 1);
if(i < end) right = helper(n, i + 1, end);
res += left * right;
}
return res;
}
}
网上看了一下,可以使用一维动态规划。res[i]代表n=i时,不同排列数量。res[i] = res[0]*res[i-1] + res[1]*res[n-2] ......
其中res[0]*res[n-1]代表i做root时,左边0个点,右边n-1个点时的排列数。在考虑解法时,要忽略掉数字。
public class Solution {
/**
* @paramn n: An integer
* @return: An integer
*/
public int numTrees(int n) {
if(n == 0 || n == 1) return 1;
int[] res = new int[n + 1];
res[0] = 1;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < i; j++) {
res[i] += res[j] * res[i - 1 - j];
}
}
return res
;
}
}
how many structurally unique BSTs
(binary search trees) that store values 1...n?
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
分析:递归法。n=19时超时.
public class Solution {
/**
* @paramn n: An integer
* @return: An integer
*/
public int numTrees(int n) {
if(n == 0 || n == 1) return 1;
return helper(n, 1, n);
}
int helper(int n, int start, int end) {
if(start == end) return 1;
int res = 0;
for(int i = start; i <= end; i++) {
int left = 1, right = 1;
if(i > start) left = helper(n, start, i - 1);
if(i < end) right = helper(n, i + 1, end);
res += left * right;
}
return res;
}
}
网上看了一下,可以使用一维动态规划。res[i]代表n=i时,不同排列数量。res[i] = res[0]*res[i-1] + res[1]*res[n-2] ......
其中res[0]*res[n-1]代表i做root时,左边0个点,右边n-1个点时的排列数。在考虑解法时,要忽略掉数字。
public class Solution {
/**
* @paramn n: An integer
* @return: An integer
*/
public int numTrees(int n) {
if(n == 0 || n == 1) return 1;
int[] res = new int[n + 1];
res[0] = 1;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < i; j++) {
res[i] += res[j] * res[i - 1 - j];
}
}
return res
;
}
}
相关文章推荐
- **[Lintcode]Unique Binary Search Trees II 不同的二叉查找树 II
- LeetCode96_Unique Binary Search Trees(求1到n这些节点可以组成多少种不同的二叉查找树) Java题解
- 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】
- 【二叉查找树】02不同的二叉查找树个数II【Unique Binary Search Trees II】
- lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
- LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解
- LeetCode:Unique Binary Search Trees(二叉查找树)
- LeetCode题库解答与分析——#95. 不同的二叉查找树 IIUniqueBinarySearchTreeII
- Unique Binary Search Trees--lintcode
- 【LeetCode】Unique Binary Search Trees 二叉查找树的个数
- Unique Binary Search Trees II --lintcode
- LeetCode--Unique Binary Search Trees (不同二叉搜索树的个数)Python
- lintcode-medium-Unique Binary Search Trees
- Unique Binary Search Trees-计算表示相同序列的不同BST个数
- 能构成的二叉查找树的个数 Unique Binary Search Trees
- lintcode:Unique Binary Search Trees II
- LeetCode OJ 之 Unique Binary Search Trees II (不同的二叉搜索树 - 二)
- LeetCode 96 Unique Binary Search Trees不同的二叉搜索树的个数
- [LeetCode]Unique Binary Search Trees
- [LeetCode] Unique Binary Search Trees