vector<TreeNode *>* recusive(int start,int end) { vector<TreeNode *> *vl; vector<TreeNode *> *vr; vector<TreeNode *> *v; TreeNode *root; v = new vector<TreeNode *>(); if(end < start){ v->push_back(NULL); return v; } for(int i = start; i <= end; ++i){ vl = recusive(start,i-1); vr = recusive(i+1,end); int l = vl->size(); int r = vr->size(); for(int j = 0; j < l; ++j) for(int k = 0; k < r; ++k){ root = new TreeNode(i); root->left = (*vl)[j]; root->right = (*vr)[k]; v->push_back(root); } delete vl; delete vr; } return v; }
void recusive(int idx, int target, vector<int>& candidates) { if(target==0) { ret.push_back(vec); return; } if(target<0) return; for( int i=idx; i<candidates.size(); ++i) { int val = candidates[i]; vec.push_back(val); recusive(i, target-val, candidates); vec.pop_back(); } }
vector<TreeNode *> generateTrees(int n) { vector<TreeNode *> *ret = recusive(1,n); return *ret; }
vector<vector<int>> combinationSum(vector<int>& candidates, int target) { sort( candidates.begin(), candidates.end()); recusive(0, target, candidates); return ret; }