您的位置:首页 > 产品设计 > UI/UE

【leetcode】Unique Binary Search Trees I & II

2015-04-07 21:45 344 查看
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.注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root!不要想当然地将某个点作为root时,认为其他所有节点都能全部放在left/right中,除非这个点是 min 或者 max 的。
1         3     3      2      1
\       /     /      / \      \
3     2     1      1   3      2
/     /       \                 \
2     1         2                 3
分析思路:n个点中每个点都可以作为root,当 i 作为root时,小于 i 的点都只能放在其左子树中,大于 i 的点只能放在右子树中,此时只需求出左、右子树各有多少种,二者相乘即为以 i 作为root时BST的总数。解题技巧:求数量,要首先想到使用动态规划(dp)如果不只是求数量,还要把所有的树都枚举出来,就要使用dfs(深度优先搜索)来遍历决策树了。这道题是使用动态规划来解决的。那么如何去求这个问题的状态转移方程呢?其实大部分动态规划的难点都是求状态转移方程。n=0时,为空树,那么dp[0]=1;n=1时,显然也是1,dp[1]=1;n=2时,dp[2]=2;对于n>2时,dp
=dp[0]*dp[n-1]+dp[1]*dp[n-2]+......+dp[n-1]*dp[0]典型的动态规划题目,因为有重叠的子问题,当前决策依赖于子问题的解。同时,当根节点元素为 1, 2, 3, 4, 5, ..., i, ..., n时,基于以下原则的BST树具有唯一性:
以i为根节点时,其左子树构成为[0,...,i-1],其右子树构成为[i+1,...,n]构成。Python:
class Solution:
def numTrees(self,n):
dp=[1,1,2]
if n<=2:
return dp

else:
dp+= [0 for i in range(n-2)]
for i in range(3,n+1):
for j in range(1,i+1):
dp[i]+=dp[j-1]*dp[i-j]
return dp
C++:
class Solution {
public:
int numTrees(int n) {
vector<int> num;
num.push_back(1);
for(int i=1;i<=n;i++)
{
num.push_back(0);
if(i<3)
num[i]=i;
else
{
for(int j=1;j<=i;j++)
num[i]+=num[j-1]*num[i-j];
}
}

return num
;
}
};
这道题的模型正好是卡特兰数的定义。可以用卡特兰数的通项公式来求解,这样时间复杂度就可以降低到O(n)。在其他文章对 卡特兰数 进行稍微浅显的认识。II:Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,
Given n = 3, your program should return all 5 unique BST's shown below.
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
return createTree(1,n);
}

vector<TreeNode*> createTree(int start,int end)
{
vector<TreeNode *> result;
if(start>end)
{
result.push_back(NULL);
return result;
}

for(int i=start;i<=end;i++)
{
vector<TreeNode*> left=createTree(start,i-1);
vector<TreeNode*> right=createTree(i+1,end);
for(int j=0;j<left.size();j++)
{
for(int k=0;k<right.size();k++)
{
TreeNode *root=new TreeNode(i);
root->left=left[j];
root->right=right[k];
result.push_back(root);
}
}
}

return result;
}
};

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: