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. WPF Dispatcher 一次小重构

    几个月之前因为项目需要,需要实现一个类似于WPF Dispatcher类的类,来实现一些线程的调度.之前因为一直做Asp.Net,根本没有钻到这个层次去,做的过程中,诸多不顺,重构了四五次,终于实现, ...

  2. SQLSERVER--定期清理维护作业的历史记录

    刚删除一个数据库时,在清理数据库备份历史记录时,执行超过近10分钟还未完成,随时查了下,吓死宝宝啦,逻辑读操作竟然高达8000万次以上! 通过UI进行删除数据库时,会默认勾选上“删除数据库备份和还原历 ...

  3. java web module of login

    Reffer to the book<java web整合开发王者归来>. It's jsp page. Offer the values of username and password ...

  4. mysql 5.7 win7 压缩版安装

    1.下载mysql压缩版并解压: 2.复制my-defualt.ini , 命名为my.ini; 3. 3.1 运行在下图bin目录下运行:mysqld --install   安装mysql服务: ...

  5. SVG的路径动画效果

    使用SVG animateMotion实现的一个动画路径效果,相关代码如下. 在线调试唯一地址:http://www.gbtags.com/gb/debug/c88f4099-5056-4ad7-af ...

  6. java基础学习总结——java环境变量配置

    前言 学习java的第一步就要搭建java的学习环境,首先是要安装JDK,JDK安装好之后,还需要在电脑上配置"JAVA_HOME”."path”."classpath& ...

  7. Android组件Spinner使用

    Spinner组件是Android当中非常常用的一种用于下拉选择的组件. 本blog当中主要解决的几个问题: 如何在XML中初始化Spinner选项 如何使用代码的方式初始化Spinner选项 一个A ...

  8. AP6181 正基 WIFI 模块

    a. Module size: 12*12mm (pin to pin compatible) Package: Stamp type 44pins AP6181: WiFiAP6210: WiFi/ ...

  9. 【CUDA学习】GPU硬件结构

    GPU的硬件结构,也不是具体的硬件结构,就是与CUDA相关的几个概念:thread,block,grid,warp,sp,sm. sp: 最基本的处理单元,streaming processor  最 ...

  10. iptables不生效解决办法

    修改完iptables之后,如果不生效,需要修改一下这个参数 echo 1 > /proc/sys/net/ipv4/ip_forward   使iptables转发开启生效,如果设置为0,则不 ...