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?

https://leetcode.com/problems/recover-binary-search-tree/

二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树。

最简单的办法,中序遍历二叉树生成序列,然后对序列中排序错误的进行调整。最后再进行一次赋值操作,但这个不符合空间复杂度要求。

需要用两个指针记录错误的那两个元素,然后进行交换。

怎样找出错误的元素?遍历二叉排序树,正确时应该是从小到大,如果出现之前遍历的节点比当前大,则说明出现错误。所以我们需要一个pre指针来指向之前经过的节点。

如果只有一处不符合从小到大,则只用交换这一个地方。第二个指针记录第二个异常点。

Github repository: https://github.com/huashiyiqike/myleetcode

//JAVA CODE:
public class Solution {
TreeNode first = null, second = null, pre = null;
//first larger than follow, second smaller than pre
public void helper(TreeNode root){
if(root.left != null) helper(root.left);
if(pre != null && root.val < pre.val){
if(first == null)
first = pre;
second = root;
}
pre = root;
if(root.right != null) helper(root.right);
}
public void recoverTree(TreeNode root) {
helper(root);
int tmp = first.val;
first.val = second.val;
second.val = tmp;
}
}
//C++ CODE:
#include <iostream>
#include <cstdlib>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
TreeNode *first = NULL, *second = NULL, *last = NULL;
public:
void inorder(TreeNode* root){
if(root->left != NULL){
inorder(root->left);
}
if(last != NULL && root->val < last->val){
if(first == NULL) first = last;
second = root;
}
last = root;
if(root->right != NULL) inorder(root->right);
}
void recoverTree(TreeNode* root) {
if(root == NULL) return;
inorder(root);
if(first && second)
std::swap(first->val, second->val);
}
};
#PYTHON CODE:
class Solution:
def inorderTravel(self, root, last):
if root.left:
last = self.inorderTravel(root.left, last)
if last and last.val > root.val:
if self.first is None:
self.first = last
self.second = root
last = root
if root.right:
last = self.inorderTravel(root.right, last)
return last def recoverTree(self, root):
if root is None:
return
self.first, self.second = None, None
self.inorderTravel(root, None)
self.first.val, self.second.val = self.second.val, self.first.val

Recover Binary Search Tree--leetcode难题讲解的更多相关文章

  1. Recover Binary Search Tree [LeetCode]

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

  2. Recover Binary Search Tree leetcode java

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  3. Leetcode 笔记 99 - Recover Binary Search Tree

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

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

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

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

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

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

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

  7. 【leetcode】Recover Binary Search Tree

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

  8. 【LeetCode练习题】Recover Binary Search Tree

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

  9. LeetCode: Recover Binary Search Tree 解题报告

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

  10. 【LeetCode】99. Recover Binary Search Tree

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

随机推荐

  1. C++中虚析构函数作用

    我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数.可是,为什么要这样做呢?下面用一个小例子来说明:        有下面的两个类: class ClxBase{public:    ...

  2. 微软BI 之SSIS 系列 - 在 SSIS 中导入 ACCESS 数据库中的数据

    开篇介绍 来自 天善学院 一个学员的问题,如何在 SSIS 中导入 ACCESS 数据表中的数据. 在 SSIS 中导入 ACCESS 数据库数据 ACCESS 实际上是一个轻量级的桌面数据库,直接使 ...

  3. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  4. 我们可以用SharePoint做什么

    前言 不知不觉作为一个SharePoint的开发人员若干年了,从SharePoint api 开始学习,到了解SharePoint的结构,逐渐一点点了解sharepoint的体系:从SharePoin ...

  5. 【软件架构】IM架构设计(安卓版)

    1. 架构总览 2. 模块介绍 2.1 协议封装与任务流程 2.1.1 协议与任务的封装 协议有协议头(协议头因为格式相同,被抽象出来)和协议体组成,协议有两类:请求协议(request)和回复协议( ...

  6. paip.解决中文url路径的问题图片文件不能显示

    paip.解决中文url路径的问题图片文件不能显示 #现状..中文url路径 图片文件不能显示 <img src="img/QQ截图20140401175433.jpg" w ...

  7. Spring 之autowired

    Spring中autowired主要用于装配树形值,其关键类为BeanWrapperImpl,阅读代码发现其关键方法setPropertyValue有如下一段代码. PropertyHandler p ...

  8. MySQL基础操作(一)

    1. 连接数据库mysql -h localhost -u root -p 123456 2. 查看数据库版本select version(); 3. 查看数据库支持的存储引擎和当前默认存储引擎sho ...

  9. 【VerySky原创】如何查找SNRO编号范围的使用情况;

    SAP所有编号范围的对象都可以在表NRIV中找到:

  10. JVM中的Stack和Frame

    JVM执行Java程序时需要装载各种数据,比如类型信息(Class).类型实例(Instance).常量数据(Constant).本地变量等.不同的数据存放在不同的内存区中,这些数据内存区称作“运行时 ...