题目:

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. vs2008 c++工程如何设置生成调试信息

    记录一个使用vs2008碰到的问题: 今天在用vs2008的时候,想封装一个lib库,建了一个c++的lib工程,后来为了测试函数功能,想偷懒就直接在工程中加了个main函数,并且把工程属性prope ...

  2. C语言函数参数既做出参又做入参的代表

    //使用fcntl对文件进行加锁 #include "stdio.h"#include "unistd.h"#include "fcntl.h&quo ...

  3. javascript 代码优化工具 UglifyJS

    安装: 1. 安装 node.js 环境 (这个不用我教了吧,网上教程一大堆哦.) 2. 进入 https://github.com/mishoo/UglifyJS  右上角 “Download” Z ...

  4. 浏览器内核-Webkit

    关键字:浏览器内核,浏览器引擎,Browser,Webkit,Blink,Chromium. 本文简单介绍一下各种浏览器内核.着种介绍一下Webkit.顾名思义,浏览器内核就是浏览器的核心部分,也可以 ...

  5. Oracle监听器—动态注册

    注册就是将数据库作为一个服务注册到监听程序.客户端不需要知道数据库名和实例名,只需要知道该数据库对外提供的服务名就可以申请连接到数据库.这个服务名可能与实例名一样,也有可能不一样. 注册分: 1. 静 ...

  6. 半小时学会上传本地项目到github

    半小时学会上传本地项目到github 闲着无聊写给那些正在学习怎么上传本地项目到github的同学. 开始学习 一.创建github账号 好吧,这步多余了. 二.创建个人仓库 三.配置SSH keys ...

  7. easy ui datagrid 数据绑定

    1.前台页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  8. JVM的组成部分与内存管理

    JVM的组成部分与内存管理 JVM区域划分 由于Java程序是交由JVM执行的,所以我们在谈Java内存区域划分的时候事实上是指JVM内存区域划分.在讨论JVM内存区域划分之前,先来看一下Java程序 ...

  9. kruskal算法-Pascal

    马上就快要考试了,然而突然发现自己图论已经废了,于是再都打一遍练练手...... const maxn=; maxe=maxn*maxn; type edge=record //edge记录每一条边, ...

  10. 回顾Ado.Net

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...