Creating binary tree for 1 to i-1 and inserting i in each tree at every possible position

interview-questions
Tags: #<Tag:0x00007f24278683a8>

#1

void copi(TreeNode* &T, TreeNode* node)
{
if(node->left!=NULL)
{
T->left=new TreeNode(node->left->val);
copi(T->left,node->left);
}
if(node->right!=NULL)
{
T->right=new TreeNode(node->right->val);
copi(T->right,node->right);
}
return;
}
vector<TreeNode*> Solution::generateTrees(int A) {
TreeNode T=new TreeNode(1);
vector<TreeNode
> Ans;
Ans.push_back(T);
for(int j=1;j<A;j++)
{
vector<TreeNode*> temp;
for(int i=0;i<Ans.size();i++)
{
TreeNode* A=new TreeNode(j+1);
A->left=Ans[i];
TreeNode K=A;
temp.push_back(K);
TreeNode
Temp=Ans[i];
while(Temp!=NULL)
{
TreeNode* X=Temp->right;
Temp->right=new TreeNode(j+1);
Temp->right->left=X;
TreeNode *K=new TreeNode(Ans[i]->val);
copi(K,Ans[i]);
temp.push_back(K);
Temp->right=X;
Temp=Temp->right;
}
}
Ans.assign(temp.begin(),temp.end());
}
return Ans;
}