I split the problem into two parts:

- A generator that create a sequence of nodes from a pre-order traversal.
- The flatten method uses the generator to fix the tree, just link prev.right with curr.

Is recursive, but nice, and the code is reusable, easy to understand and maintain.

Complessity: O(n)?

```
class Solution:
# @param root, a tree node
# @return nothing, do it in place
def flatten(self, root):
if not root: return
prev = None
for node in self.preOrder(root):
node.left = None
if prev: prev.right = node
prev = node
def preOrder(self, node):
left = node.left
right = node.right
yield node
if left:
for node in self.preOrder(left):
yield node
if right:
for node in self.preOrder(right):
yield node
```