```
class Solution {
public:
void flatten(TreeNode* root) {
if(!root) return;
if(root->right) mystack.push(root->right);
root->right = root->left;
root->left = NULL;
flatten(root->right);
if(!mystack.empty()) {
root->right = mystack.top();
mystack.pop();
flatten(root->right);
}
}
private:
stack<TreeNode* > mystack;
};
```