https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
class Solution:
def Convert(self, pRootOfTree):
# write code here
if pRootOfTree == None:
return None
def find_right(node):
while node.right:
node = node.right
return node
leftNode = self.Convert(pRootOfTree.left)
rightNode = self.Convert(pRootOfTree.right)
retNode = leftNode
if leftNode:
leftNode = find_right(leftNode)
else:
retNode = pRootOfTree
pRootOfTree.left = leftNode
pRootOfTree.right = rightNode
if leftNode != None:
leftNode.right = pRootOfTree
if rightNode != None:
rightNode.left = pRootOfTree
return retNode