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. linux下confstr与uname函数_获取C库与内核信息

    #include <stdio.h> #include <sys/utsname.h> //uname int main(int argc, char **argv[]) { ...

  2. Unity异常警告错误处理方法

    原地址:http://www.haogongju.net/art/2591936 1.  The AnimationClip 'cube1_anim' used by the Animation co ...

  3. centos7,py2和py3共存

    1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用 python -V 命令查看一下是否安 ...

  4. Spring整合Activiti工作流

    代码地址如下:http://www.demodashi.com/demo/11911.html 一. 前期准备 安装必要的开发环境 eclipse/intellij+maven 3.5.x + tom ...

  5. LINPACK測试

    1简单介绍 LINPACK是线性系统软件包(Linear system package) 的缩写. Linpack如今在国际上已经成为最流行的用于測试高性能计算机系统浮点性能的benchmark.通过 ...

  6. spring启动加载过程源码分析

    我们知道启动spring容器两常见的两种方式(其实都是加载spring容器的xml配置文件时启动的): 1.在应用程序下加载 ApplicationContext ctx = new ClassPat ...

  7. python元组、列表的异同总结

    定义的异同: 列表(list):[] list是一种有序的集合,能够随时加入和删除当中的元素.用 [] 表示. 列表的三个特性:①创建之后也能够加减改动元素. ②元素能够是数字.字符.变量等.也能够混 ...

  8. ImageBox Control with Zoom/Pan Capability

    Download source files - 10.8 Kb Download demo project - 6.81 Kb Introduction This control extends th ...

  9. ExtjS学习--------Ext.define定义类

    Ext类Class的配置项:(注:Extjs的 的中文版帮助文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 ExtJS配置文件和演 ...

  10. Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle

    Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle 1. 主键1 2. uniq  index2 3.  ...