题目:

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://leetcode.com/problems/recover-binary-search-tree/

题解:

恢复BST。和上一题validate BST一样,可以使用recursive的 in-order traversal。 先递归查找左子树里root.val < prevNode.val的,确定第一个逆序的节点,之后继续搜索,查找第二个逆序的节点。注意不可以在第一次找到第二个逆序的时候就返回了,要继续查找。否则像[2, 3, 1]这样的case会通不过,因为直接就把2和3 swap了,并没有找到1。 最后把两个节点的val交换一下就可以了。

Time Complexity - O(n), Space Complexity - O(1)。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
TreeNode firstNode;
TreeNode secondNode;
TreeNode lastNode; public void recoverTree(TreeNode root) {
this.lastNode = new TreeNode(Integer.MIN_VALUE);
inorderTraversal(root);
int tmp = firstNode.val;
firstNode.val = secondNode.val;
secondNode.val = tmp;
} private void inorderTraversal(TreeNode root) {
if(root == null)
return;
inorderTraversal(root.left);
if(firstNode == null && this.lastNode.val >= root.val)
firstNode = lastNode;
if(firstNode != null && this.lastNode.val >= root.val)
secondNode = root;
this.lastNode = root;
inorderTraversal(root.right);
}
}

Update:

想了一下,不用初始化lastNode = new TreeNode(Integer.MIN_VALUE)。 当然初始化也可以,it doesn't matter。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
TreeNode firstNode;
TreeNode secondNode;
TreeNode lastNode; public void recoverTree(TreeNode root) {
inorderTraversal(root);
int tmp = firstNode.val;
firstNode.val = secondNode.val;
secondNode.val = tmp;
} private void inorderTraversal(TreeNode root) {
if(root == null)
return;
inorderTraversal(root.left);
if(lastNode != null && firstNode == null && lastNode.val >= root.val)
firstNode = lastNode;
if(lastNode != null && firstNode != null && lastNode.val >= root.val)
secondNode = root;
lastNode = root;
inorderTraversal(root.right);
}
}

Reference:

https://leetcode.com/discuss/13034/no-fancy-algorithm-just-simple-and-powerful-order-traversal

https://leetcode.com/discuss/7319/an-elegent-time-complexity-and-space-complexity-algorithm

https://leetcode.com/discuss/41182/tree-deserializer-and-visualizer-for-python

https://leetcode.com/discuss/31543/a-concise-java-solution-using-morris-inorder-o-time-o-1-space

https://leetcode.com/discuss/26310/detail-explain-about-morris-traversal-finds-incorrect-pointer

http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html

99. Recover Binary Search Tree的更多相关文章

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

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

  2. Leetcode 笔记 99 - Recover Binary Search Tree

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

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

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

  4. 【LeetCode】99. Recover Binary Search Tree

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

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

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

  6. leetcode 99 Recover Binary Search Tree ----- java

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

  7. LeetCode OJ 99. Recover Binary Search Tree

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

  8. 【一天一道LeetCode】#99. Recover Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...

  9. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

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

随机推荐

  1. sqlplus 可以登录 plsql 不能登录

    最开始我以为是system用户被锁定了,但是解锁后仍然不可以登录.大神指导之后可以了,说是缺少监听器,解决过程如下: 1.将“tnsnames.ora”和“listener.ora”两个文件里的“lo ...

  2. (poj 3177) Redundant Paths

    题目链接 :http://poj.org/problem?id=3177 Description In order to <= F <= ,) grazing fields (which ...

  3. vs2010创建COM以及调用

    1,创建COM组件 2,调用COM 3,MFC调用COM

  4. Check Big/Little Endian

    Little endian:Low memory address stores low byte value.(eg.  short int 0x2211   0xbfd05c0e->0x11 ...

  5. 连接Oracle数据库的OracleHelper.cs

    using System; using System.Configuration; using System.Data; using System.Data.OracleClient; using S ...

  6. DTcms 导航选中样式以及简化方法

    (建议使用方法2,执行效率略高) 一般用于导航不能循环输出的情况. 可以循环输出导航的情况直接用if判断即可. 首页模版中顶部,自定义c#代码. <%set string channel=&qu ...

  7. 转载 C# BindingSource

    1.引言 BindingSource组件是数据源和控件间的一座桥,同时提供了大量的API和Event供我们使用.使用这些API我们可以将Code与各种具体类型数据源进行解耦:使用这些Event我们可以 ...

  8. 页面有什么隐藏bug:字体,图片

    字体: 一行(太长)-display:inline-block,text-overflow: ellipsis;max-width:xxpx 多行(太高,太矮)-设置max-height,min-he ...

  9. Linux 服务器如何修改 DNS

    个人一直用的阿里云的ECS,在配置环境的时候就想着修改一下默认的 DNS,听说阿里云自己的 DNS 速度和稳定性都不错,所以就将默认的 DNS 修改成了阿里云的DNS,并一直稳定使用,下面给出修改方法 ...

  10. Linux分类笔记(一)-权限管理

    Linux分类笔记(一) 权限管理 普通权限 文件的普通权限 对一个普通的文件使用ls -ll命令后,看到下面的输出内容   而对于文件权限中的每一位,又分别代表了以下的意思 文件类型又有以下几类: ...