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?

分析:http://www.cnblogs.com/yuzhangcmu/p/4208319.html

具体的思路,还是通过中序遍历,只不过,不需要存储每个节点,只需要存一个前驱即可。

例如1,4,3,2,5,6

1.当我们读到4的时候,发现是正序的,不做处理

2.但是遇到3时,发现逆序,将4存为第一个错误节点,3存为第二个错误节点

3.继续往后,发现3,2又是逆序了,那么将第2个错误节点更新为2

如果是这样的序列:1,4,3,5,6同上,得到逆序的两个节点为4和3。

========================================

这里我们补充一下,为什么要替换第二个节点而不是第一个节点:
e.g. The correct BST is below:
 
The inorder traversal is :  1 3 4 6 7 8 10 13 14

Find the place which the order is wrong.
        Wrong order: 1 3 8 6 7 4 10 13 14     
        FIND:                    8 6
        Then we find:             7 4
        8, 6 是错误的序列, 但是,7,4也是错误的序列。
        因为8,6前面的序列是正确的,所以8,6一定是后面的序列交换来的。
        而后面的是比较大的数字,也就是说8一定是被交换过来的。而7,4
        中也应该是小的数字4是前面交换过来的。

用反证法来证明:
        假设:6是后面交换过来的
        推论: 那么8比6还大,那么8应该也是后面交换来的,
        这样起码有3个错误的数字了
        而题目是2个错误的数字,得证,只应该是8是交换过来的。
结论就是:我们需要交换的是:8, 4.

 public class Solution {
TreeNode pre = null, first = null,second = null; public void recoverTree(TreeNode root) {
inOrder(root);
int tmp = first.val;
first.val = second.val;
second.val = tmp;
} public void inOrder(TreeNode root) {
if (root == null) return;
inOrder(root.left);
if (pre != null && pre.val > root.val) {
if (first == null) {
first = pre;
second = root;
} else {
second = root;
}
}
pre = root;
inOrder(root.right);
}
}

Recover Binary Search Tree的更多相关文章

  1. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  2. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  3. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  4. 【LeetCode练习题】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  5. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  6. [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆

    Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...

  7. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  8. 【LeetCode】99. Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  9. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  10. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. 捕获EF提交异常

    try { } catch (DbEntityValidationException dbex) { string errMsg = string.Empty; foreach (var eve in ...

  2. webuploader横向按钮样式

    #picker{display: inline-block;line-height: 1.428571429;vertical-align: middle;margin: 0 12px 0 0;wid ...

  3. Jedis 例子(demo)大全

    第一步:到git下载jedis源码,如果你用maven或者gradle,那么直接下官方的即可,地址:https://github.com/xetorthio/jedis:如果你用ant,下载这个:ht ...

  4. Struts2 action的单例与多例

    struts 2的Action是多实例的并非单例,也就是每次请求产生一个Action的对象.原因是:struts 2的Action中包含数据,例如你在页面填写的数据就会包含在Action的成员变量里面 ...

  5. 如何才能实现在点击链接时直接在网页中打开word文档,但不提示保存

    一般要直接打开需要客户端 1.客户端有word支持 2.客户端浏览器的版本与设置 可寻找一下相关的控件或中间件,我的意见是看能否变通一下,把word转成HTML或PDF再展示给用户.(若用户不需要编辑 ...

  6. 从HTML原型到jsp页面完美转型攻略(教你即使不会写代码也能弄出漂亮的网页)

    大家都知道软件项目(web)开发之前都要先做原型设计,而我们使用的比较多的一款原型设计软件就是Axure rp了.在Axure rp上画原型不需要任何编码能力,而且生成的原型可以在浏览器上运行.除了没 ...

  7. windows 杂项

    农企amd的Radeon: 蓝宝石-镭 显卡m330只是 m200的马甲版本, nvidia的Geforce: ge: geometry : 几何很强, 精视 radeon倾向于单精度浮点计算很强, ...

  8. 请问如何查看mysql 的端口号?

    mysql> show variables like 'port'; +---------------+-------+ | Variable_name | Value | +--------- ...

  9. 关于linux vi命令替换的使用说明

    转自:http://os.51cto.com/art/200907/140987.htm vi/vim 中可以使用 :s 命令来替换字符串.以前只会使用一种格式来全文替换,今天发现该命令有很多种写法( ...

  10. PostgreSQL建立分区表示例

    pgsql 分区表: --主表 create table test(id integer, name varchar(32)); create index idx_test_id on test us ...