I am basically following convert binary search tree to doubly linked list article but I am getting TLE. Could anyone tell me what I am doing wrong?

```
void flatten(TreeNode *root, TreeNode* &prev) {
if (!root) return;
flatten(root->left, prev);
if (prev) prev->right = root;
TreeNode* right = root->right;
root->left = prev;
prev = root;
root->right = nullptr;
flatten(right, prev);
}
void flatten(TreeNode *root) {
TreeNode* prev = nullptr;
flatten(root, prev);
}
```