```
class Solution {
public:
void flatten(TreeNode* root) {
if (!root) return;
flatten(root->left);
flatten(root->right);
auto ln = root->left;
while (ln && ln->right) ln = ln->right;
if (ln) {
ln->right = root->right;
root->right = root->left;
}
root->left = NULL;
}
};
```