LeetCode99-恢复二叉搜索树

LeetCode99-恢复二叉搜索树

题目

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

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

示例二

解法

树节点的定义


/**
 * Definition for a binary tree node.
 * public 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;
 *     }
 * }
 */

方法一
O(n)的空间复杂度,O(n)的时间复杂度,用中序遍历,遍历出的节点插入队列,之后在队列中寻找出错的两个节点。

注意:
引入flag标志位,是为了消除两个互换位置的节点是队列中前后两个的关系

class Solution {
    List<TreeNode> list = new ArrayList<>();
    int pre = Integer.MIN_VALUE;
    public void recoverTree(TreeNode root) {
        helper(root);   //获得中序遍历的节点顺序
        int flag = 0 ;
        int len = list.size();
        TreeNode l = null;  //第一个错位
        TreeNode r = null;  //第二个错位
        TreeNode pre = list.get(0);
        for(int i = 1 ; i < len ; ++i){
            TreeNode cur = list.get(i);
            if(cur.val<pre.val){
                if(flag==0){
                    l = pre;
                    r = cur;
                    ++flag;
                    pre = cur;
                    continue;
                }
                if(flag == 1){
                    r = cur;
                    break;
                }  
            }
            pre = cur;
        }
        int temp = l.val;
        l.val = r.val;
        r.val = temp;
    }
    public void helper(TreeNode root){
        if(root == null) return;
        helper(root.left);
        list.add(root);
        helper(root.right);
    }
}

方法二:
空间复杂度是O(1),不用额外存放节点,根据方法一改进而来,在中序遍历的过程中找到两个节点,找到第二个节点之后剪枝

class Solution {
    int pre = Integer.MIN_VALUE;
    int flag = 0 ;  //标志位,flag==0,说明还没有找到第一个错位,flag==1,说明找到第一个错位、正在找第二个错位
    TreeNode preNode = null;
    TreeNode l = null;  //第一个错位
    TreeNode r = null;  //第二个错位
    public void recoverTree(TreeNode root) {
        //找到第一个节点
        TreeNode a = root;
        while(a!=null){
            preNode = a;
            a = a.left;
        }
        pre = preNode.val;
        helper(root);   
        int temp = l.val;
        l.val = r.val;
        r.val = temp;
    }

    //中序遍历
    public void helper(TreeNode root){
        if(root == null) return;
        helper(root.left);
        int cur = root.val;
        if(cur < pre){
            if(flag == 0){
                l = preNode;
                r = root;
                ++flag;
            }else{
                r = root;
                return;
            }
        }
        pre = cur;
        preNode = root;
        helper(root.right);
    }
}

方法三:Morris 中序遍历
Morris 遍历算法整体步骤如下(假设当前遍历到的节点为 xx):

如果 x 无左孩子,则访问 x 的右孩子,即 x=x.right。
如果 x 有左孩子,则找到 x 左子树上最右的节点(即左子树中序遍历的最后一个节点,x 在中序遍历中的前驱节点),我们记为 predecessor。根据 predecessor 的右孩子是否为空,进行如下操作。
(1)如果 predecessor 的右孩子为空,则将其右孩子指向 x,然后访问 x 的左孩子,即 x=x.left。
(2)如果 predecessor 的右孩子不为空,则此时其右孩子指向 x,说明我们已经遍历完 x 的左子树,我们将 predecessor 的右孩子置空,然后访问 x 的右孩子,即 x = x.right。
重复上述操作,直至访问完整棵树。

class Solution {
    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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值