【题目描述】
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
【思路】
1.中序遍历,返回遍历列表。中序遍历二叉搜索树正好可以得到排序列表。
2.对遍历列表中的每个节点,添加左右指针。
【代码】
class Solution:
def Convert(self, pRootOfTree):
list1=self.zx(pRootOfTree)
if(len(list1)==0):
return None
if(len(list1)==1):
return pRootOfTree
for i in range(len(list1)-1):
list1[i].right=list1[i+1]
list1[i+1].left=list1[i]
return list1[0]
def zx(self,root):
if not root:
return []
return self.zx(root.left)+[root]+self.zx(root.right)