99. 恢复二叉搜索树

python 复制代码
#中序遍历,寻找插值位置并交换
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def recoverTree(self, root: Optional[TreeNode]) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        #记录中序遍历的值
        nums = []
        self.inorder(root, nums)
        print(nums)
        x,y = self.swappos(nums)
        self.swap(root,2,x,y)

        #找到替换值的位置
    def inorder(self, root, nums):
        if root:
            self.inorder(root.left,nums)
            nums.append(root.val)
            self.inorder(root.right,nums)

    def swappos(self, nums):
        n = len(nums)
        index1 = -1
        index2 = -1
        for i in range(n-1):
            if nums[i+1] < nums[i]:
                index1 = i+1
                if index2 == -1:
                    index2 = i
                else:
                    break
        x,y = nums[index2], nums[index1]
        return [x,y]
    def swap(self, root, count, num1, num2):
        if root:
            if root.val == num1 or root.val == num2:
                root.val = num2 if root.val == num1 else num1
                count -= 1
                if count == 0:
                    return
            self.swap(root.left, count, num1, num2)
            self.swap(root.right, count, num1, num2)
python 复制代码
#边中序遍历,边判断

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def recoverTree(self, root):
        stack = []
        x, y = None, None
        prev = None
        #迭代形式的中序遍历:借助栈
        while stack or root:
            #遍历左子树
            while root:
                stack.append(root)
                root = root.left
            #左子树遍历完,root为null
            root = stack.pop()

            if prev and root.val < prev.val:
                x = root
                if y == None:
                    y = prev
                else:
                    break
            #这个root判断完,将其赋予prev
            prev = root
            root = root.right
        self.swap(x,y)
    def swap(self,x,y):
        tmp = y.val
        y.val = x.val
        x.val = tmp
python 复制代码
#moriss遍历
class Solution:
    def recoverTree(self, root):
        x, y, pred, predecessor = None, None, None, None

        while root:
            if root.left:
                predecessor = root.left
                while predecessor.right and predecessor.right != root:
                    predecessor = predecessor.right
                if predecessor.right == None:
                    predecessor.right = root
                    root = root.left

                else:
                    predecessor.right = None
                    root = root.right
            else:
                root = root.right
python 复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def recoverTree(self, root):
        x, y, pred, predecessor = None, None, None, None

        while root:
            if root.left:
                predecessor = root.left
                while predecessor.right and predecessor.right != root:
                    predecessor = predecessor.right
                if predecessor.right == None:
                    predecessor.right = root
                    root = root.left

                else:
                    if pred and root.val < pred.val:
                        #y为后面那个值
                        y = root
                        if not x:
                            x = pred
                    pred = root

                    predecessor.right = None
                    root = root.right
            else:
                if pred and root.val < pred.val:
                    y = root
                    if not x:
                        x = pred
                pred = root
                root = root.right
        x.val,y.val = y.val,x.val

                

                
相关推荐
程序猿000001号11 分钟前
探索Python的pytest库:简化单元测试的艺术
python·单元测试·pytest
engchina42 分钟前
如何在 Python 中忽略烦人的警告?
开发语言·人工智能·python
清梦20201 小时前
经典问题---跳跃游戏II(贪心算法)
算法·游戏·贪心算法
Dream_Snowar2 小时前
速通Python 第四节——函数
开发语言·python·算法
西猫雷婶2 小时前
python学opencv|读取图像(十四)BGR图像和HSV图像通道拆分
开发语言·python·opencv
Altair澳汰尔2 小时前
数据分析和AI丨知识图谱,AI革命中数据集成和模型构建的关键推动者
人工智能·算法·机器学习·数据分析·知识图谱
A懿轩A2 小时前
C/C++ 数据结构与算法【栈和队列】 栈+队列详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·栈和队列
汪洪墩2 小时前
【Mars3d】设置backgroundImage、map.scene.skyBox、backgroundImage来回切换
开发语言·javascript·python·ecmascript·webgl·cesium
Python机器学习AI2 小时前
分类模型的预测概率解读:3D概率分布可视化的直观呈现
算法·机器学习·分类
吕小明么3 小时前
OpenAI o3 “震撼” 发布后回归技术本身的审视与进一步思考
人工智能·深度学习·算法·aigc·agi