Leetcode 99. 恢复二叉搜索树

题目

二叉搜索树中的两个节点被错误地交换。

请在不改变其结构的情况下,恢复这棵树。

在这里插入图片描述在这里插入图片描述
进阶:

  • 使用 O(n) 空间复杂度的解法很容易实现。
  • 你能想出一个只使用常数空间的解决方案吗?

解答

本题需要找到两个需要交换的结点使得二叉搜索树结构恢复。

思路:如若按照中序遍历的序列来讲,问题就转化为找中序遍历结果的两个逆序对。

中序遍历算法的复杂度如下:

  • 递归:O(n) 时间,O(n) 空间。
  • 栈:O(n) 时间,O(n) 空间。
  • Mirros 算法:O(n) 时间,O(1) 空间。

遍历方式一:递归

代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    TreeNode firstNode = null;
    TreeNode secondNode = null;
    TreeNode pre = new TreeNode(Integer.MIN_VALUE);
    
    public void recoverTree(TreeNode root) {
        inOrder(root);
    
        int temp = firstNode.val;
        firstNode.val = secondNode.val;
        secondNode.val = temp;
    }

    private void inOrder(TreeNode node) {
        if (node == null) return;
        
        inOrder(node.left);
        
        if(firstNode == null && pre.val > node.val) firstNode = pre;
        if(firstNode != null && pre.val > node.val) secondNode = node;
        pre = node;
        
        inOrder(node.right);
    }
}
结果

在这里插入图片描述

遍历方式二:栈

代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void recoverTree(TreeNode root) {
        TreeNode firstNode = null;
        TreeNode secondNode = null;
        LinkedList<TreeNode> stack = new LinkedList<>();
        TreeNode pre = new TreeNode(Integer.MIN_VALUE);   
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            
            cur = stack.pop();
            
            if(firstNode == null && pre.val > cur.val) firstNode = pre;
            if(firstNode != null && pre.val > cur.val) secondNode = cur;
            pre = cur;
            
            cur = cur.right;
        }
        
        int temp = firstNode.val;
        firstNode.val = secondNode.val;
        secondNode.val = temp;
    }
}
结果

在这里插入图片描述

遍历方式三:Mirros算法

代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void recoverTree(TreeNode root) {
        TreeNode firstNode = null;
        TreeNode secondNode = null;
        TreeNode cur = root;
        TreeNode last = null;
        TreeNode pre = new TreeNode(Integer.MIN_VALUE);  
        while(cur != null) {
            if(cur.left == null) {
                if(firstNode == null && pre.val > cur.val) firstNode = pre;
                if(firstNode != null && pre.val > cur.val) secondNode = cur;
                pre = cur;
                cur = cur.right;
            } else {
                last = cur.left;
                while(last.right != null && last.right != cur) {
                    last = last.right;
                }
                
                if(last.right == null) {
                    last.right = cur;
                    cur = cur.left;
                }
                
                if(last.right == cur) {
                    last.right = null;
                    if(firstNode == null && pre.val > cur.val) firstNode = pre;
                    if(firstNode != null && pre.val > cur.val) secondNode = cur;
                    pre = cur;
                    cur = cur.right;
                }
            }
        }
       
        int temp = firstNode.val;
        firstNode.val = secondNode.val;
        secondNode.val = temp;
    }
}
结果

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值