```
TreeNode* function(TreeNode *node)
{
if(node == NULL)
return NULL;
if(node->left == NULL && node->right == NULL)
return node;
if(node->left == NULL)
return function(node->right);
TreeNode *lastRight = node->right;
node->right = node->left;
TreeNode *l = function(node->left);
if(l != NULL)
l->right = lastRight;
TreeNode *r = function(lastRight);
return r;
}
void flatten(TreeNode *root) {
function(root);
}
```