二叉搜索树与双向链表 牛客网 剑指Offer

牛客网 剑指Offer

  • 题目描述
  • 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def Convert(self,pRootOfTree):
        if pRootOfTree == None:
            return None
        if not pRootOfTree.left and not pRootOfTree.right:
            return pRootOfTree
        left = pRootOfTree.left
        if left:
            self.Convert(left)
            while left.right:
                left = left.right
            pRootOfTree.left,left.right = left,pRootOfTree
            
        right = pRootOfTree.right
        if right:
            self.Convert(right)
            while right.left:
                right = right.left
            pRootOfTree.right, right.left = right,pRootOfTree
            
        while pRootOfTree.left:
            pRootOfTree = pRootOfTree.left
        return pRootOfTree

 

你可能感兴趣的:(Algorithm,算法,牛客网,剑指Offer)