According to the hint, the subject is equivalent to the pre order Traverse traversal, and in turn all nodes are saved to right child, and the left child is defined as an empty set. With recursive thinking, then if we separate the left and right subtree flatten into a list, we have:
1
/ \
2 5
\ \
3 6 <-Righttail
\
4 <-Lefttail
So use recursive solution one: note that since the right subtree is finally connected to the back of the Zuozi, save the head of the right subtree with temp.
1 defFlatten (self, root):2 """3 : Type Root:treenode4 : rtype:void do not return anything, modify root in-place instead.5 """ 6 if notRoot:7 return 8 Self.flatten (root.left)9 Self.flatten (root.right)Ten Onetemp =Root.right A -Root.right =Root.left -Root.left =None the - whileRoot.right: -Root =Root.right - +Root.right = Temp
Leetcode, Flatten Binary Tree to Linked List