【Leecode】Leecode刷题之路第99天之恢复二叉搜索树

题目出处

99-恢复二叉搜索树-题目出处

题目描述


个人解法

思路:

java 复制代码
todo

代码示例:(Java)

java 复制代码
todo

复杂度分析

java 复制代码
todo

官方解法

99-恢复二叉搜索树-官方解法

方法1:显式中序遍历

思路:

代码示例:(Java)

java 复制代码
@Data
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution1 {
    public void recoverTree(TreeNode root) {
        List<Integer> nums = new ArrayList<Integer>();
        inorder(root, nums);
        int[] swapped = findTwoSwapped(nums);
        recover(root, 2, swapped[0], swapped[1]);
    }

    public void inorder(TreeNode root, List<Integer> nums) {
        if (root == null) {
            return;
        }
        inorder(root.left, nums);
        nums.add(root.val);
        inorder(root.right, nums);
    }

    public int[] findTwoSwapped(List<Integer> nums) {
        int n = nums.size();
        int index1 = -1, index2 = -1;
        for (int i = 0; i < n - 1; ++i) {
            if (nums.get(i + 1) < nums.get(i)) {
                index2 = i + 1;
                if (index1 == -1) {
                    index1 = i;
                } else {
                    break;
                }
            }
        }
        int x = nums.get(index1), y = nums.get(index2);
        return new int[]{x, y};
    }

    public void recover(TreeNode root, int count, int x, int y) {
        if (root != null) {
            if (root.val == x || root.val == y) {
                root.val = root.val == x ? y : x;
                if (--count == 0) {
                    return;
                }
            }
            recover(root.right, count, x, y);
            recover(root.left, count, x, y);
        }
    }


}

复杂度分析

  • 时间复杂度:O(N),其中 N 为二叉搜索树的节点数。中序遍历需要 O(N) 的时间,判断两个交换节点在最好的情况下是 O(1),在最坏的情况下是 O(N),因此总时间复杂度为 O(N)。
  • 空间复杂度:O(N)。我们需要用 nums 数组存储树的中序遍历列表。

方法2:隐式中序遍历

思路:


代码示例:(Java)

java 复制代码
public class Solution2 {
    public void recoverTree(TreeNode root) {
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
        TreeNode x = null, y = null, pred = null;

        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if (pred != null && root.val < pred.val) {
                y = root;
                if (x == null) {
                    x = pred;
                } else {
                    break;
                }
            }
            pred = root;
            root = root.right;
        }

        swap(x, y);
    }

    public void swap(TreeNode x, TreeNode y) {
        int tmp = x.val;
        x.val = y.val;
        y.val = tmp;
    }


}

复杂度分析

  • 时间复杂度:最坏情况下(即待交换节点为二叉搜索树最右侧的叶子节点)我们需要遍历整棵树,时间复杂度为 O(N),其中 N 为二叉搜索树的节点个数。
  • 空间复杂度:O(H),其中 H 为二叉搜索树的高度。中序遍历的时候栈的深度取决于二叉搜索树的高度。

方法3:Morris 中序遍历

思路:


代码示例:(Java)

java 复制代码
public class Solution3 {
    public void recoverTree(TreeNode root) {
        TreeNode x = null, y = null, pred = null, predecessor = null;

        while (root != null) {
            if (root.left != null) {
                // predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
                predecessor = root.left;
                while (predecessor.right != null && predecessor.right != root) {
                    predecessor = predecessor.right;
                }

                // 让 predecessor 的右指针指向 root,继续遍历左子树
                if (predecessor.right == null) {
                    predecessor.right = root;
                    root = root.left;
                }
                // 说明左子树已经访问完了,我们需要断开链接
                else {
                    if (pred != null && root.val < pred.val) {
                        y = root;
                        if (x == null) {
                            x = pred;
                        }
                    }
                    pred = root;

                    predecessor.right = null;
                    root = root.right;
                }
            }
            // 如果没有左孩子,则直接访问右孩子
            else {
                if (pred != null && root.val < pred.val) {
                    y = root;
                    if (x == null) {
                        x = pred;
                    }
                }
                pred = root;
                root = root.right;
            }
        }
        swap(x, y);
    }

    public void swap(TreeNode x, TreeNode y) {
        int tmp = x.val;
        x.val = y.val;
        y.val = tmp;
    }


}

复杂度分析

  • 时间复杂度:O(N),其中 N 为二叉搜索树的高度。Morris 遍历中每个节点会被访问两次,因此总时间复杂度为 O(2N)=O(N)。
  • 空间复杂度:O(1)。

考察知识点

1.二叉树的中序遍历

收获

Gitee源码位置

99-恢复二叉搜索树-源码

相关推荐
qunaa01013 分钟前
基于改进YOLO11-ASF-P2的多旋翼无人机检测识别系统_红外航拍目标检测算法优化_1
算法·目标检测·无人机
Xの哲學6 分钟前
Linux 页回收机制深度剖析: 从设计哲学到实战调试
linux·服务器·网络·算法·边缘计算
蜗牛^^O^8 分钟前
传统网关与云原生网关
java·服务器·云原生
幽络源小助理8 分钟前
Yolo-Seg实例分割自动标注工具-幽络源原创
算法·yolo·实例分割·自动标注
阿杰真不会敲代码13 分钟前
webSocket入门
java·网络·spring boot·websocket·网络协议
木风小助理17 分钟前
Kotlin内联函数及其关联关键字的深度解析
android·java·开发语言
刘一说21 分钟前
Java语言多态特性在Spring Boot中的体现:从原理到实战
java·开发语言·spring boot
橘颂TA24 分钟前
【剑斩OFFER】算法的暴力美学——LeetCode 703 题:数据流中的第 K 大元素
网络·算法·结构与算法
郑州光合科技余经理26 分钟前
同城O2O系统架构解析:中台化如何赋能本地生活服务
java·开发语言·javascript·人工智能·系统架构·php·生活
qq_124987075327 分钟前
基于微信小程序的垃圾分类信息系统(源码+论文+部署+安装)
java·前端·spring boot·后端·微信小程序·小程序·计算机毕业设计