Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure. (Hard)

Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

分析:

BST的中序遍历应该是递增的,我们考虑这样一个中序遍历序列1,2,6,4,5,3,7,其中3,6是交换了位置的。

所以我们就按照中序遍历的顺序遍历树,记录cur, prev, beforePrev三个变量;

第一次出现before < prev > cur的prev即为要交换的第一个元素,最后一个满足beforePrev > prev < cur的即为要交换的另一个元素。

然后再遍历一遍把这两个节点找出来,交换其value值即可。

注意:比如1,0这种样例,当最后一个节点是被交换的元素的时候,无法用上述判断,但如果其满足prev > cur说明cur即为要交换的元素。

代码:

 /**
* 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 {
int beforePrev = -0x7FFFFFFF, prev = -0x7FFFFFFF, cur = -0x7FFFFFFF;
int s1 = -0x7FFFFFFF, s2 = -0x7FFFFFFF;
TreeNode* t1;
TreeNode* t2;
private:
void helper(TreeNode* root) {
if (root == nullptr) {
return;
}
helper(root -> left);
beforePrev = prev;
prev = cur;
cur = root -> val;
if (beforePrev < prev && prev > cur && s1 == -0x7FFFFFFF) {
s1 = prev;
}
if (beforePrev > prev && prev < cur ) {
s2 = prev;
} helper(root -> right);
} void dfs(TreeNode* root) {
if (root == nullptr) {
return;
}
dfs(root -> left);
if (root -> val == s1) {
t1 = root;
}
if (root -> val == s2) {
t2 = root;
}
dfs(root -> right);
}
public:
void recoverTree(TreeNode* root) {
helper(root);
if (cur < prev) {
s2 = cur;
}
dfs(root);
swap(t1 -> val, t2 -> val);
return;
}
};
 

LeetCode99 Recover Binary Search Tree的更多相关文章

  1. Leetcode 笔记 99 - Recover Binary Search Tree

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

  2. 【leetcode】Recover Binary Search Tree

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

  3. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  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: Recover Binary Search Tree 解题报告

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

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

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

  7. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  8. 【LeetCode】99. Recover Binary Search Tree

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

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

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

随机推荐

  1. 大量的Close_wait 发现的 too many open file 错

    突然频繁出现大量的 Close_wait,查看程序日志,发现 connection 在 accept 时报错,Too many open file. 估计程序里有这个漏洞,当 accept 时报错,没 ...

  2. SPSS详细操作:样本均数间的多重比较

    SPSS详细操作:样本均数间的多重比较 下面我们用例子来探讨:方差分析得出各组总体均数间的差异有统计学意义之后,如何进行样本均数间的多重比较. 一.问题与数据 为调查A.B.C三种治疗措施对患者谷丙转 ...

  3. webpack4进阶配置

    移动端CSS px自动转换成rem 需要两步来实现: px2rem-loader 在构建阶段将px转换成rem lib-flexible 页面渲染时动态计算根元素的font-size值(手机淘宝开源库 ...

  4. CentOS 6.8 Java 环境搭建

      1.搜索 Java 1.7 64 2.下载 文件 3.Xshell 安装lrzsz 4.选择路径 5.使用 rz 命令选择上传 6.打开 /etc/profile vim /etc/profile ...

  5. Python科学计算生态圈--Scipy

  6. Laravel(PHP)使用Swagger生成API文档不完全指南 - 基本概念和环境搭建 - 简书

    在PHPer中,很多人听说过Swagger,部分人知道Swagger是用来做API文档的,然而只有少数人真正知道怎么正确使用Swagger,因为PHP界和Swagger相关的资料实在是太少了.所以鄙人 ...

  7. SQL竖表转横表Json数据

    1.数据准备 create  table  Vertical(  Id  int ,  ProjectName varchar(20),  ProjectValue int ) insert into ...

  8. arcgis点密度专题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Python2.7用sys.stdout.write实现打印刷新

    如何能在控制台实现在一行中显示进度的信息呢,就像使用pip安装时的进度那样. 如果用print则会打印成多行,下面这个小技巧可以在一行中打印: import time import sys if __ ...

  10. Springboot项目下mybatis报错:Invalid bound statement (not found)

    mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...