99. 恢复二叉搜索树——Java实现

题目描述:

给你二叉搜索树的根节点 root ,该树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。

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

示例 1:
在这里插入图片描述
输入:root = [1,3,null,null,2]
输出:[3,1,null,null,2]
解释:3 不能是 1 左孩子,因为 3 > 1 。交换 1 和 3 使二叉搜索树有效。

示例 2:
在这里插入图片描述

输入:root = [3,1,4,null,null,2]
输出:[2,1,4,null,null,3]
解释:2 不能在 3 的右子树中,因为 2 < 3 。交换 2 和 3 使二叉搜索树有效。

提示:

树上节点的数目在范围 [2, 1000] 内
-231 <= Node.val <= 231 - 1

Java代码:

核心代码:

	public TreeNode x = null;
    public TreeNode y = null;
    public TreeNode pre = null;

    public void traverse(TreeNode root){
        if(root == null)
            return;

        traverse(root.left);

        if(pre == null){
            pre = root;
        }else{
            if(pre.val > root.val){
                y = root;
                if(x == null){
                    x = pre;
                }
            }
            pre = root;
        }

        traverse(root.right);
    }

    public void recoverTree(TreeNode root) {
        if(root == null)
            return;
        
        traverse(root);

        if(x!=null && y!=null){
            int temp = x.val;
            x.val = y.val;
            y.val = temp;
        }
    }

全部代码:

package com.renxia;

public class nineNine {
    public static TreeNode x = null;
    public static TreeNode y = null;
    public static TreeNode pre = null;

    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(){};

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

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

    public static void traverse(TreeNode root){
        if(root == null)
            return;

        traverse(root.left);

        if(pre == null){
            pre = root;
        }else{
            if(pre.val > root.val){
                y = root;
                if(x == null){
                    x = pre;
                }
            }
            pre = root;
        }

        traverse(root.right);
    }

    public static void recoverTree(TreeNode root){
        if(root == null)
            return;

        traverse(root);

        if(x!=null && y!=null){
            int temp = x.val;
            x.val = y.val;
            y.val = temp;
        }

    }

    public static void inOrder(TreeNode root){
        if(root == null)
            return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        node1.left = node3;
        node3.right = node2;

        System.out.println("前:");
        inOrder(node1);
        System.out.println();

        recoverTree(node1);
        System.out.println("后:");
        inOrder(node1);
    }
}

运行结果:

【用中序遍历验证结果的正确性】
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值