```
void flatten(TreeNode* root) {
if (!root)
return;
TreeNode* p = root;
while (p)
{
if (!p->left)
{
p = p->right;
}
else
{
TreeNode* tmp = p->left;
while(tmp->right != nullptr)
tmp = tmp->right;
tmp->right = p->right;
p->right = p->left;
p->left = nullptr;
}
}
}
```