题目
链表转二叉树
思路
找到链表的中点,然后递归
代码
def linkedlist_tree(link):
if link is None:
return None
dummy = Node_L(None,0)
dummy.next = link
link = dummy
fast = link
slow = link
pre = link
while fast and fast.next is not None:
pre = slow
fast = fast.next.next
slow = slow.next
pre.next = None
print(pre.value)
root = Node(slow.value)
root._left = linkedlist_tree(link.next)
root._right = linkedlist_tree(slow.next)
return root