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
分析:依次把每个节点作为根节点,左边节点作为左子树,右边节点作为右子树,那么总的数目等于左子树数目*右子树数目,实际只要求出前半部分节点作为根节点的树的数目,然后乘以2(奇数个节点还要加上中间节点作为根的二叉树数目)
递归代码:为了避免重复计算子问题,用数组保存已经计算好的结果.
class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int nums[n+1]; //nums[i]表示i个节点的二叉查找树的数目
memset(nums, 0, sizeof(nums));
return numTreesRecur(n, nums);
}
int numTreesRecur(int n, int nums[])
{
if(nums[n] != 0)return nums[n];
if(n == 0){nums[0] = 1; return 1;}
int tmp = (n>>1);
for(int i = 1; i <= tmp; i++)
{
int left,right;
if(nums[i-1])left = nums[i-1];
else left = numTreesRecur(i-1, nums);
if(nums[n-i])right = nums[n-i];
else right = numTreesRecur(n-i, nums);
nums[n] += left*right;
}
nums[n] <<= 1;
if(n % 2 != 0)
{
int val;
if(nums[tmp])val = nums[tmp];
else val = numTreesRecur(tmp, nums);
nums[n] += val*val;
}
return nums[n];
}
};
非递归代码
:从0个节点的二叉查找树数目开始自底向上计算,dp方程为nums[i] = sum(nums[k-1]*nums[i-k]) (k = 1,2,3...i)
class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int nums[n+1]; //num[i]表示i个节点的二叉查找树数目
memset(nums, 0, sizeof(nums));
nums[0] = 1;
for(int i = 1; i <= n; i++)
{
int tmp = (i>>1);
for(int j = 1; j <= tmp; j++)
nums[i] += nums[j-1]*nums[i-j];
nums[i] <<= 1;
if(i % 2 != 0)
nums[i] += nums[tmp]*nums[tmp];
}
return nums[n];
}
};
15年9月13号更新
-------------------------
首先注意这里是
BST而不是普通的BT,所以数字的大小会对插入的位置有影响,当n为3时,下面的这个树是不符合题意的,因为1不大于2。这类找combination/permutation的题目都需要好好找规律。
2
\
3
/
1
n = 0
n = 1
1
n = 2
1 2
\ /
2 1
n = 3
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
定义f(n)为unique BST的数量,以n = 3为例:
构造的BST的根节点可以取{1, 2, 3}中的任一数字。
如以1为节点,则left subtree只能有0个节点,而right subtree有2, 3两个节点。所以left/right subtree一共的combination数量为:f(0) * f(2) = 2
以2为节点,则left subtree只能为1,right subtree只能为2:f(1) * f(1) = 1
以3为节点,则left subtree有1, 2两个节点,right subtree有0个节点:f(2)*f(0) = 2
总结规律:
f(0) = 1
f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-2)*f(1) + f(n-1)*f(0)
class Solution {
public:
int numTrees(int n) {
vector<int> numBST(n+1,0);
numBST[0] = 1;
for(int i=1; i<=n; i++) {
for(int j=0; j<i; j++) {
numBST[i] += numBST[j]*numBST[i-1-j];
}
}
return numBST[n];
}
};