leetcode-95. 不同的二叉搜索树 II
题目:
代码:
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode{
int val;
TreeNode *left,*right;
};
vector<TreeNode*>gTrees(int start,int end){
if(start>end){
return {NULL};
}
vector<TreeNode*> res;
for(int i=start;i<=end;i++){
vector<TreeNode*>leftTrees=gTrees(start,i-1);
vector<TreeNode*>rightTrees=gTrees(i+1,end);
for(auto& left:leftTrees){
for(auto& right:rightTrees){
TreeNode *currTree=new TreeNode;
currTree->val=i;
currTree->left=left;
currTree->right=right;
res.push_back(currTree);
}
}
}
return res;
}
vector<TreeNode*> generateTrees(int n) {
if(n==0){
return {};
}
return gTrees(1,n);
}
int main(){
vector<TreeNode*> res;
int n;
cin>>n;
res=generateTrees(n);
return 0;
}
|