【每日一题-leetcode】99. 恢复二叉搜索树

99. 恢复二叉搜索树

  1. 恢复二叉搜索树

Difficulty: 困难

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

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

示例 1:

输入: [1,3,null,null,2]

1
/
3
\
2

输出: [3,1,null,null,2]

3
/
1
\
2

示例 2:

输入: [3,1,4,null,null,2]

3
/ \
1   4
/
2

输出: [2,1,4,null,null,3]

2
/ \
1   4
/
3

进阶:

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

Solution

Language: 全部题目

1.迭代

// 对于树的解决方案,基本是都是在做遍历的时候 进行数据操作。因此,本题采用中序遍历。
// 中序遍历是一个有序的 左中右。 那么对于一个无序的 需要我们修改的中序结果为 3,1,null,null,2
// 可以分析到 3 1 是我们交换的位置,
// 1.找到第一个 既 第一个大于后一个的值即为 firstNode 
// 2.第二个 在找到第一个的前提下,前一个大于后一个。即为secondNode
// 好了,我们来分析一下时间复杂度 即为O(n) 只需要遍历一遍树即可。
public void recoverTree(TreeNode root) {
    if(root == null) return;
    TreeNode p1 = null;
    TreeNode p2 = null;
    TreeNode p = new TreeNode(Integer.MIN_VALUE);

    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode cur = root;
    
    while(cur != null || !stack.isEmpty()){
        while(cur != null){
            stack.push(cur);
            cur = cur.left;
        }
        cur = stack.pop();
        //找到第一个节点
        if(p1 == null && p.val > cur.val) p1 = p;
        //找到第二个节点
        if(p1 != null && p.val > cur.val) p2 = cur;
        p = cur;
        cur = cur.right;
    }
    int tmp = p1.val;
    p1.val = p2.val;
    p2.val = tmp;
}

2.递归


	TreeNode p1 = null;
    TreeNode p2 = null;
    TreeNode p = new TreeNode(Integer.MIN_VALUE);

    public void recoverTree(TreeNode root) {
        in_order(root);    
        int t = p1.val;
        p1.val = p2.val;
        p2.val = t;
    }

    public void in_order(TreeNode r){
        if(r == null) return;
        in_order(r.left);
        if(p1 == null && p.val > r.val) p1 = p;
        if(p1 != null && p.val > r.val) p2 = r;
        p = r;
        in_order(r.right);
    }





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qxlxi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值