1. BST
2. 动态规划
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> generateTrees(int begin, int end)
{
vector<TreeNode*> result;
if(begin > end)
{
result.push_back(NULL);
return result;
}
if(begin == end)
{
TreeNode* node = new TreeNode(begin);
result.push_back(node);
return result;
}
for(int i=begin;i<=end;i++)
{
vector<TreeNode*> res_left = generateTrees(begin, i-1);
vector<TreeNode*> res_right = generateTrees(i+1, end);
for(int j=0;j<res_left.size();j++)
{
for(int k=0;k<res_right.size();k++)
{
TreeNode* newRoot = new TreeNode(i);
newRoot->left = res_left[j];
newRoot->right = res_right[k];
result.push_back(newRoot);
}
}
}
return result;
}
vector<TreeNode*> generateTrees(int n) {
vector<TreeNode*> result;
if(n==0)
return result;
else
{
return generateTrees(1,n);
}
}
};