Chase southwest 信用卡值得留么?# Money - 海外理财
f*m
1 楼
leetcode 上的Unique Binary Search Trees,算总的Binary Search Trees数目:
Given n, how many structurally unique BST's (binary search trees) that store
values 1...n?
可以用dp:
int dp[n+1];
memset(dp, 0, (n+1)*sizeof(int));
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];
若是要算总的Trees数目,那应该是
dp[i] += 2*dp[j]*dp[i-j-1]吧?
既一个节点既可以出现在根的左边,又可以出现在右边?
Given n, how many structurally unique BST's (binary search trees) that store
values 1...n?
可以用dp:
int dp[n+1];
memset(dp, 0, (n+1)*sizeof(int));
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];
若是要算总的Trees数目,那应该是
dp[i] += 2*dp[j]*dp[i-j-1]吧?
既一个节点既可以出现在根的左边,又可以出现在右边?