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?

confused what "{1,#,2,3}" means?

>
read more on how binary tree is serialized on OJ.

这里有几个知识点能够注意一下。是个不错的考察BST的题目。

一、要怎样找出这两个有乱序的位置呢?假设直接在树上考察,似乎关系有些模糊;那么,考虑一个性质。通过中序遍历,能够将BST按升序输出,eg:12
3 4 56 7,这里随意调换一下位置。构成16 3 4 5 2 7, 6 和
2将数列切割成了三份。每份独立时仍然保持升序(有能够能头尾的部门不包括元素),可是6 > 3。 5 > 2;说明,第一次出现 pre > cur的情况。pre是那个错误节点。第二次出现 pre > cur的情况。cur是错误节点,记录下来就能够了。

二、有可能调换位置的元素在排序中相邻(树结构图中不一定相邻),一中提到的pre >cur的情况就发生重合,仅仅有一次;

代码例如以下:

class Solution {
private:
TreeNode *Pre, *node1, *node2; //申明为成员变量,这样每一个函数都能够訪问,不必反复创建; void inOrder(TreeNode *root){
if (root == NULL)
return;
inOrder(root->left);
if (Pre != NULL && Pre->val > root->val){ //第二次(也是最后一次)出现的才是正确位置。这里这样写避免了推断,直接覆盖
node2 = root;
if (node1 == NULL) //利用 NULL 为是否为第一次出现的标记
node1 = Pre;
}
Pre = root;
inOrder(root->right);
} public:
void recoverTree(TreeNode *root) {
Pre = NULL;
node2 = node1 = NULL; inOrder(root); node1->val ^= node2->val;
node2->val ^= node1->val;
node1->val ^= node2->val;
}
};

LeetCode具体分析 :: Recover Binary Search Tree [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

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

  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】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [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

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

  7. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  8. 【LeetCode 99】Recover Binary Search Tree

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

  9. LeetCode OJ 99. Recover Binary Search Tree

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

随机推荐

  1. Spark下载与入门(Spark自学二)

    2.1 下载Spark 略 2.2 Spark中Python和Scala的shell Spark shell可用来与分布式存储在许多机器的内存或者硬盘上的数据进行交互,并且处理过程的分发由Spark自 ...

  2. 2017.11.15 hashmap的工作原理

    参考来自:http://blog.csdn.net/jeffleo/article/details/54946424 一 hashMap的基本概念 1.HashMap的定义 public class ...

  3. maven 插件之 AutoConfig 工具使用笔记

    AutoConfig 是一款 maven 插件,主要用于 Maven 项目打包使用.在我们的工作中,会将自己写的代码打成 jar 包或者 war 包发布到各种环境上.一般地,不用的环境所使用的数据库. ...

  4. proto3 中的 map 类型

    .proto syntax = "proto3"; option optimize_for = SPEED; message TestStruct { map<int32,s ...

  5. vue - 总结build.js

    1. 导入外部包,用关键字 const :ES2015->不可变量 内部使用变量,let -> 块级声明 2.无分号结尾(我猜大概是用了es6变量,避免了es5应缺少分号出现的一些问题) ...

  6. Django——正则表达式的举例与基本语法

    如果想在URLconf中加入URL和view,只需增加映射URL模式和view功能的Python tuple即可. 这里演示如何添加view中hello功能. from django.conf.url ...

  7. mongoDB 高级查询之复杂查询$where

    http://blog.csdn.net/drifterj/article/details/7833883

  8. shll 基础讲解

    http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html Shell编程基础 $# 命令行得到的参数个数 $@ 命令行得到的所有参数作 ...

  9. android-gradle-深入浅出-五:build type

    默认情况下,Android插件自动为项目构建一个debug和一个release版本的应用.这两个版本的不同主要体现在在非开发机上的调试功能以及APK的签名方式.debug版本使用一个用公开的name/ ...

  10. TortoiseSVN客户端使用方法

    SVN对于程序开发来说是非常重要的东西,它是非常不错的版本管理工具,下面介绍一下TortoiseSVN客户端的使用方法. 工具/原料 TortoiseSVN 方法/步骤   如果没有TortoiseS ...