采用递归的方法实现,具体如下:
def Tree2List(root):
if not root:
return None, None
if root.left:
head, _tail = Tree2List(root.left)
_tail.right = root
root.left = _tail.right
else:
head = root
if root.right:
_head, tail = Tree2List(root.right)
root.right = _head
_head.left = root
else:
tail = root
return head, tail