Nice classical problem


#1
> /**
>  * Definition for binary tree
>  * struct TreeNode {
>  *     int val;
>  *     TreeNode *left;
>  *     TreeNode *right;
>  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
>  * };
>  */
>  
> vector<TreeNode*> construct(int s, int e)
> {
>     vector<TreeNode*> send;
>     
>     if(s > e)
>     {
>         send.push_back(NULL);
>         return send;
>     }
>     
>     for(int i = s; i <= e; i++)
>     {
>         vector<TreeNode*> left = construct(s, i-1);
>         vector<TreeNode*> right = construct(i+1, e);
>         
>         for(int j = 0; j < left.size(); j++)
>         {
>             TreeNode *l = left[j];
>             for(int k = 0; k < right.size(); k++)
>             {
>                 TreeNode *r = right[k];
>                 TreeNode *x = new TreeNode(i);
>                 x->left = l;
>                 x->right = r;
>                 send.push_back(x);
>             }
>         }
>     }
>     return send;
> }
> vector<TreeNode*> Solution::generateTrees(int A) {
>     
>     vector<TreeNode*> ans = construct(1,A);
>     
>     return ans;
> }