采用递归的方法实现,具体如下:
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
递归转换二叉树为双向链表
本文介绍了一种使用递归方法将二叉树转换为双向链表的算法实现。通过调整节点的左右指针,使每个节点的左指针指向其前驱节点,右指针指向其后继节点,最终形成一个双向链表。
3万+

被折叠的 条评论
为什么被折叠?



