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?

=============

解法:来自leetcode 150题集

O(n)的解法,开一个指针数组,中序遍历,将节点指针一次存放在数组中,

然后寻找两处逆向的位置,先从前往后找第一个逆序的位置,然后从后往前寻找第二个逆序的位置,交换两个指针的值,

递归/非递归中序遍历一般需要用到栈,空间也是O(n)的,可以利用morris中序遍历的方式

=====

code:

/**
* 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:
///
void recoverTree(TreeNode *root){
pair<TreeNode *,TreeNode *> broken;
TreeNode *curr = root;
TreeNode *prev = nullptr;
broken.first = broken.second = nullptr; while(curr!=nullptr){
if(curr->left==nullptr){
detect(broken,prev,curr);
prev = curr;
curr = curr->right;
}else{
auto node = curr->left;
///prev = curr->left;
while(node->right != nullptr && node->right!=curr){
node = node->right;
} ///find predecessor
if(node->right==nullptr){
node->right = curr;
curr = curr->left;
}else{
node->right = nullptr;
detect(broken,prev,curr);
prev = curr;
curr = curr->right;
}
}///if-else
}///while swap(broken.first->val,broken.second->val);
}
void detect(pair<TreeNode *,TreeNode *> &broken,TreeNode *prev,
TreeNode *curr){
if(prev!=nullptr && prev->val > curr->val){
if(broken.first == nullptr) broken.first = prev;
broken.second = curr;
}
}
};

99. Recover Binary Search Tre的更多相关文章

  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(复原BST) ☆☆☆☆☆

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

  4. 【LeetCode】99. 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 复原二叉搜索树

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

  6. leetcode 99 Recover Binary Search Tree ----- java

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

  7. 99. Recover Binary Search Tree

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

  8. LeetCode OJ 99. Recover Binary Search Tree

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

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

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

随机推荐

  1. 快速掌握grep命令及正则表达式

    Linux系统自带了支持拓展正则表达式的 GNU 版本 grep 工具,所有的Linux发行版中均默认安装grep ,grep 命令被用来检索一台服务器或工作站上任何位置的文本信息,如何在 Linux ...

  2. Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. js/jsp清空表单2种方法

    js方式清空表单数据的两种方式 方法1:遍历页面元素 /* 清空FORM表单内容  id:表单ID*/  function ClearForm(id) {     var objId = docume ...

  4. 使用密钥验证方式登录linux系统

    1.使用PuTTY工具PuTTYgen生成密钥对,打开PuTTYgen,点击Generate生成公钥(生成过程动动鼠标会提升进度哦). 2.将公钥(蓝色的是私钥)放到服务器上去: -在/root目录下 ...

  5. STL概述

    一.关于STL STL(Standard Template Library,标准模板库)是C++语言标准中的重要组成部分.STL 以模板类和模板函数的形式为程序员提供了各种数据结构和算法的精巧实现,程 ...

  6. Vue.js相关知识2-组件

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. ZOJ-3870 Team Formation

    题目大意:给n个正数,找出满足A^B>max(A,B)的对数. 题目分析: 代码如下: # include<iostream> # include<cstdio> # i ...

  8. Linux驱动设计——字符设备驱动(一)

    Linux字符设别驱动结构 cdev结构体 struct cdev { struct kobject kobj; struct module *owner; const struct file_ope ...

  9. 【转】 iOS 开发之静态库.a和动态库详解 -- 不错

    原文网址:http://blog.csdn.net/lxl_815520/article/details/52154331 一, 简单介绍 1.什么是库 库是程序代码的集合,是共享程序代码的一种方式 ...

  10. Linux-某电商网站流量劫持案例分析与思考

    [前言] 自腾讯与京东建立了战略合作关系之后,笔者网上购物就首选京东了.某天在家里访问京东首页的时候突然吃惊地发现浏览器突然跳到了第三方网站再回到京东,心里第一个反应就是中木马了. 竟然有这样的事,一 ...