```
class Solution {
public:
TreeNode* helper(TreeNode* root)
{
TreeNode* left = root->left, *right = root->right;
TreeNode* tail = root;
if (left)
{
tail = helper(left);
root->right = left;
root->left = NULL;
}
if (right)
{
TreeNode* rtail = helper(right);
tail->right = right;
tail = rtail;
}
return tail;
}
void flatten(TreeNode* root)
{
if (!root) return;
TreeNode* tail = helper(root);
tail->right = NULL;
}
};
```