LeetCode 99. Recover Binary Search Tree(修复二叉搜索树)

49 篇文章 0 订阅
23 篇文章 0 订阅

原题网址:https://leetcode.com/problems/recover-binary-search-tree/

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O( n ) space is pretty straight forward. Could you devise a constant space solution?

方法一:发现有错误顺序则交换,知道全部正确。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private List<TreeNode> errors = new ArrayList<>();
    private int min(TreeNode root) {
        if (root.left != null) return min(root.left);
        return root.val;
    }
    private int max(TreeNode root) {
        if (root.right != null) return max(root.right);
        return root.val;
    }
    private TreeNode rightmost(TreeNode root) {
        if (root.right != null) return rightmost(root.right);
        return root;
    }
    private TreeNode leftmost(TreeNode root) {
        if (root.left != null) return leftmost(root.left);
        return root;
    }
    boolean swapped = false;
    private void swap(TreeNode node1, TreeNode node2) {
        int temp = node1.val;
        node1.val = node2.val;
        node2.val = temp;
        swapped = true;
    }
    private void check(TreeNode root) {
        if (root.left != null) {
            if (root.left.val >= root.val) {
                swap(root, root.left);
            }
            check(root.left);
            if (max(root.left) >= root.val) {
                TreeNode rightmost = rightmost(root.left);
                swap(root, rightmost);
            }
        }
        if (root.right != null) {
            if (root.right.val <= root.val) {
                swap(root, root.right);
            }
            check(root.right);
            if (min(root.right) <= root.val) {
                TreeNode leftmost = leftmost(root.right);
                swap(root, leftmost);
            }
        }
    }
    public void recoverTree(TreeNode root) {
        do {
            swapped = false;
            check(root);
        } while (swapped);
    }
}

方法二:分析两种错误。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private void swap(TreeNode n1, TreeNode n2) {
        int t = n1.val;
        n1.val = n2.val;
        n2.val = t;
    }
    
    private TreeNode[] errors = new TreeNode[4];
    private int count;
    private TreeNode prev;
    private void check(TreeNode root) {
        if (count == 4) return;
        if (root.left != null) check(root.left);
        if (prev != null && prev.val > root.val) {
            errors[count++] = prev;
            errors[count++] = root;
        }
        prev = root;
        if (root.right != null) check(root.right);
    }
    
    public void recoverTree(TreeNode root) {
        check(root);
        if (count == 2) swap(errors[0], errors[1]);
        else swap(errors[0], errors[3]);
    }
}

可以把prev作为递归函数参数。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private TreeNode[] errors = new TreeNode[4];
    private int count = 0;
    private TreeNode traverse(TreeNode prev, TreeNode node) {
        if (count == 4) return node;
        if (node.left != null) prev = traverse(prev, node.left);
        if (prev != null && prev.val > node.val) {
            errors[count++] = prev;
            errors[count++] = node;
        }
        if (node.right == null) return node;
        return traverse(node, node.right);
    }
    public void recoverTree(TreeNode root) {
        if (root == null) return;
        traverse(null, root);
        if (count == 2) {
            int t = errors[0].val;
            errors[0].val = errors[1].val;
            errors[1].val = t;
        } else {
            int t = errors[0].val;
            errors[0].val = errors[3].val;
            errors[3].val = t;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值