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

                

                
相关推荐
SEVEN-YEARS1 分钟前
深入理解TensorFlow中的形状处理函数
人工智能·python·tensorflow
EterNity_TiMe_6 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
机器学习之心17 分钟前
一区北方苍鹰算法优化+创新改进Transformer!NGO-Transformer-LSTM多变量回归预测
算法·lstm·transformer·北方苍鹰算法优化·多变量回归预测·ngo-transformer
Suyuoa17 分钟前
附录2-pytorch yolov5目标检测
python·深度学习·yolo
yyt_cdeyyds28 分钟前
FIFO和LRU算法实现操作系统中主存管理
算法
alphaTao1 小时前
LeetCode 每日一题 2024/11/18-2024/11/24
算法·leetcode
kitesxian1 小时前
Leetcode448. 找到所有数组中消失的数字(HOT100)+Leetcode139. 单词拆分(HOT100)
数据结构·算法·leetcode
好看资源平台1 小时前
网络爬虫——综合实战项目:多平台房源信息采集与分析系统
爬虫·python
进击的六角龙2 小时前
深入浅出:使用Python调用API实现智能天气预报
开发语言·python
檀越剑指大厂2 小时前
【Python系列】浅析 Python 中的字典更新与应用场景
开发语言·python