96. Unique Binary Search Trees (Tree; DP)

时间:2023-12-12 13:10:02

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
 法I:后序遍历。同Unique Binary Search Trees II,只是现在要返回的是数量,而无需将子树的所有情况放在数组中返回。
class Solution {
public:
int numTrees(int n) {
if(n==) return ;
else return postOrderTraverse(,n);
} int postOrderTraverse(int start, int end)
{
if(start == end){ //递归结束条件:碰到叶子节点(没有子节点)
return ;
} int leftNum, rightNum, num=;
for(int i = start; i<=end; i++){ //iterate all roots
leftNum = ;
rightNum = ;
if(i > start){ //count left tree
leftNum = postOrderTraverse(start, i-);
}
if(i < end){ //count right tree
rightNum = postOrderTraverse(i+, end);
} //visit root: count number for each (leftTree, rightTree) pair
if(leftNum==) num+=rightNum;
else if(rightNum==) num+=leftNum;
else num+=leftNum*rightNum;
}
return num;
}
};

Result:  Time Limit Exceeded

法II:法I超时的原因是进行了很多重复计算。比如,在1-3的子树数量和4-6子树数量是相通的,因为它们的数字跨度相同。

解决方法是用动态规划存储每种数字跨度下的子数数量

class Solution {
public:
int numTrees(int n) {
if(n==) return ;
int dp[n+];
dp[] = ;
dp[] = ;
for(int i = ; i <= n; i++){
dp[i] = ; //initialize
for(int j = ; j <= i; j++){ //traverse root
if(i-j> && j>) //左、右子树都存在
dp[i] += dp[j-]*dp[i-j];
else if(j>) //only left tree
dp[i] += dp[j-];
else //only right tree
dp[i] += dp[i-j];
}
}
return dp[n];
} };