```
class Solution:
# @param root, a tree node
# @return nothing, do it in place
def flatten(self, root):
head = TreeNode(0)
head.right = root
node = head
while(node.right!=None):
node = node.right
if(node.left!=None):
end = node.left
while(end.right!=None):
end = end.right
temp = node.right
node.right = node.left
node.left = None
end.right = temp
head.right = None
```