Task24.恢复二叉搜索树

LeetCode99.恢复二叉搜索树

题目

在这里插入图片描述
在这里插入图片描述

代码

这里学习了老马的思路,点击这里查看原文。

这个题目其实就是在有序序列中交换了两个数字。

交换的位置有两种情况:

  • 相邻的两个数字交换

[ 1 2 3 4 5 ] 中 2 和 3 进行交换,[ 1 3 2 4 5 ],这样的话只产生一组逆序的数字(正常情况是从小到大排序,交换后产生了从大到小),3 2。
我们只需要遍历数组,找到后,把这一组的两个数字进行交换即可。

  • 不相邻的两个数字交换

[ 1 2 3 4 5 ] 中 2 和 5 进行交换,[ 1 5 3 4 2 ],这样的话其实就是产生了两组逆序的数字对。5 3 和 4 2。
所以我们只需要遍历数组,然后找到这两组逆序对,然后把第一组前一个数字和第二组后一个数字进行交换即完成了还原。

综上,在中序遍历中,只需要利用一个 pre 节点和当前节点比较,如果 pre 节点的值大于当前节点的值,那么就是我们要找的逆序的数字。分别用两个指针 first 和 second 保存即可。如果找到第二组逆序的数字,我们就把 second 更新为当前节点。最后把 first 和 second 两个的数字交换即可。

/** 
  * Definition for a binary tree node. 
  * public class TreeNode { 
  *     public int val; 
  *     public TreeNode left; 
  *     public TreeNode right; 
  *     public TreeNode(int x) { val = x; } 
  * } 
  */
public class Solution 
{    
    private TreeNode pre = null;    
    private TreeNode first = null;    
    private TreeNode second = null;
    
    public void RecoverTree(TreeNode root) 
    {        
        inorderTraversal(root);        
        if (first == null || second == null)            
            return;        
        int temp = first.val;        
        first.val = second.val;        
        second.val = temp;    
    }
    
    private void inorderTraversal(TreeNode root)    
    {        
        if (root == null)            
            return;        
        inorderTraversal(root.left);        
        if (pre != null && pre.val > root.val)        
        {            
            if (first == null)            
            {                
                first = pre;                
                second = root;            
            }            
            else                
                second = root;        
        }        
        else            
            pre = root;
                    
        inorderTraversal(root.right);    
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值