Recover Binary Search Tree

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?

基本知识点:二叉搜索树的中序遍历就是升序序列。

因此不满足升序的两个点就是互换的地方。设置头结点INT_MIN来保证第一次满足。

问题在于,不满足升序的地方最多一共会出现4次,例如:

INT_MIN, 4, 2, 3, 1, 5

>      >

涉及到4,2,3,1

稍作分析即可知道,第一次出现反常(>)意味着前一个结点有问题,而第二次出现反常(<)

意味着后一个结点有问题。因此交换这两个问题节点即可。

有个小问题需要注意:如果一共只有两个节点,那么两次反常在同一次比较中出现了。

为了处理这种情况,我们在第一次反常时,就把后一个结点也设置为问题节点,后续再不断更新。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* first;
TreeNode* second;
TreeNode* prev;
int last;
void recoverTree(TreeNode* root) {
first = NULL;
second = NULL;
prev = NULL;
last = INT_MIN;
inorder(root);
swap(first->val, second->val);
}
void inorder(TreeNode* root)
{
if(root == NULL)
return;
inorder(root->left);
if(root->val < last)
{
if(first == NULL)
first = prev;
second = root;
}
prev = root;
last = root->val;
inorder(root->right);
}
};

【LeetCode】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 [Hard] [Morris Traversal] [Tree]

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

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

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

  4. Leetcode 笔记 99 - Recover Binary Search Tree

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

  5. LeetCode OJ 99. Recover Binary Search Tree

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

  6. 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  7. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  8. 【LeetCode】98. Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  9. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. char *p 与char p[]

    char *p=a和char*p=&a 前者p是一个指针变量后者p是一个char型变量 char *p = "123"; 为全局数组,放在常量区,而非普通数据段(静态存储区 ...

  2. CMSIS-SVD Reference

    http://www.keil.com/pack/doc/cmsis/svd/html/modules.html SVD File Schema Levels Device Level Periphe ...

  3. MySQL审计功能

    http://blog.itpub.net/29733787/viewspace-1604392/

  4. Visual Studio IDE 背景色该为保护眼睛色

    将背景颜色改成你想要的背景颜色. 将色调改为:85.饱和度:123.亮度:205->添加到自定义颜色->在自定义颜色选定点确定   就搞定了!

  5. C语言跟内存分配方式-alloc malloc calloc

    转载:http://blog.csdn.net/ubuntulover/article/details/7581317 (1) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整 ...

  6. GOOGLE定位

    GOOGLE定位 /// <author>cxg 2018-3-27</author> unit umap; interface uses System.SysUtils, S ...

  7. java如何判断编码是否是utf8编码

    String的getBytes()方法是得到一个系统默认的编码格式的字节数组getBytes("utf-8")  得到一个UTF-8格式的字节数组 把String转换成bytes, ...

  8. 8个使用JavaScript展示图片解决方案

    1. JonDesign’s SmoothGallery 2.0 SmoothGallery demo 2. (E)2 Photo Gallery (E)2 Photo Gallery demo 3. ...

  9. es修改索引副本个数

    es修改索引副本个数 PUT index01/_settings { "number_of_replicas": 2 }

  10. OpenCV学习笔记(四十)——再谈OpenCV数据结构Mat详解

    原文:http://blog.csdn.net/yang_xian521/article/details/7107786 我记得开始接触OpenCV就是因为一个算法里面需要2维动态数组,那时候看cor ...