Recursive Approach for the question


#1

TreeNode* Solution::flatten(TreeNode* T) {
if(T==NULL||(T->left==NULL &&T->right==NULL)){
return T;
}
TreeNode *x=T->right;
T->right=flatten(T->left);
T->left=NULL;
TreeNode *temp=T->right;
if(temp==NULL){
T->right=flatten(x);
}
else{
while(temp->right!=NULL){
temp=temp->right;
}
temp->right=flatten(x);
}
return T;
}