class Solution {
public:
void flatten(TreeNode* root) {
if(root==NULL) return ;
flattenTree(root);
}
TreeNode* flattenTree(TreeNode* root) {
if(root==NULL) return NULL;
TreeNode* temp = root->right;
root->right = flattenTree(root->left);
root->left = NULL;
TreeNode* cursor = root;
while(cursor->right!=NULL){
cursor = cursor->right;
}
cursor->right = temp;
flattenTree(cursor->right);
return root;
}
};
public:
void flatten(TreeNode* root) {
if(root==NULL) return ;
flattenTree(root);
}
TreeNode* flattenTree(TreeNode* root) {
if(root==NULL) return NULL;
TreeNode* temp = root->right;
root->right = flattenTree(root->left);
root->left = NULL;
TreeNode* cursor = root;
while(cursor->right!=NULL){
cursor = cursor->right;
}
cursor->right = temp;
flattenTree(cursor->right);
return root;
}
};