```
class Solution:
# @param root, a tree node
# @return nothing, do it in place
prev = None
def flatten(self, root):
if not root:
return
self.prev = root
self.flatten(root.left)
temp = root.right
root.right, root.left = root.left, None
self.prev.right = temp
self.flatten(temp)
*
/
n
/ \
left right
\
*
*
\
p
```

The idea is very simple. Suppose n is the current visiting node, and p is the previous node of preorder traversal to n.right.

We just need to do the inorder replacement:

n.left -> NULL

n.right - > n.left

p->right -> n.right