Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST.

只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法。

交换的情况只有两种,

case1 两个节点在树中(中序遍历序列)相邻,

case2 两个节点在树中(中序遍历序列)不相邻。

只要三个指针 Pre first second 即可记录两种case

class Solution {
public:
TreeNode *first;
TreeNode *second;
TreeNode *pre; void inOrder(TreeNode *root){
if (root==NULL)
return;
inOrder(root->left);
if (pre == NULL){pre = root;}
else {
if (pre->val > root->val){
if (first==NULL) {first = pre;}
second = root;
}
pre = root;
}
inOrder(root->right);
}
void recoverTree(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
pre = NULL;
first = NULL;
second= NULL;
inOrder(root);
int val;
val = first->val;
first->val=second->val;
second->val=val;
return;
}
};

  

第五周 Leetcode 99. Recover Binary Search Tree (HARD)的更多相关文章

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

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

  2. [LeetCode] 99. Recover Binary Search 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 ----- java

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

  4. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

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

  5. Leetcode#99 Recover Binary Search Tree

    原题地址 中序遍历二叉搜索树,正常情况下所有元素都应该按递增排列,如果有元素被交换,则会出现前面元素大于后面的情况,称作反序.由于交换了两个节点,所以通常会有两处反序,但如果是两个相邻节点发生了交换, ...

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

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

  7. Leetcode 笔记 99 - Recover Binary Search Tree

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

  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】Recover Binary Search Tree

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

随机推荐

  1. Volume 1. String(uva)

    10361 - Automatic Poetry #include <iostream> #include <string> #include <cstdio> # ...

  2. 《机器学习实战》-逻辑(Logistic)回归

    目录 Logistic 回归 本章内容 回归算法 Logistic 回归的一般过程 Logistic的优缺点 基于 Logistic 回归和 Sigmoid 函数的分类 Sigmoid 函数 Logi ...

  3. url方法使用与单例模式

    一.url方法使用 from django.contrib import admin from django.urls import path, include from django.conf.ur ...

  4. GitHub总结

    1) 工作原理 2) 工作流程 clone资源到本地 更新本地资源 新增或修改clone的资源 查看状态 资源推送回github

  5. Fiddler抓取https相关设置

    转自:https://www.cnblogs.com/joshua317/p/8670923.html 很多使用fiddler抓包,对于http来说不需太多纠结,随便设置下就能用,但是抓取https就 ...

  6. POJ-2594 Treasure Exploration floyd传递闭包+最小路径覆盖,nice!

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8130   Accepted: 3 ...

  7. oracle exp direct 执行机制

    使用直接导出模式 direct=y exp 比传统模式导出快一倍 oracle提供2种模式导出表数据,传统模式CONVENTIONAL PATH和直接模式DIRECT PATH,有direct指定. ...

  8. vue2.0一个书城实例

    gitHub克隆地址 git clone https://github.com/Webxiaoyaun/vue-book.git 点击去Github下载 ## 一个书城 ## 有增加,修改,缓存,懒加 ...

  9. NOIP2018普及游记

    我好弱啊,今年又是考pj啊 今年GD的又是在我们学校有考点(gzez) 考前其实还是蛮紧张的,毕竟考砸了就AFO了.我dp是真的弱,模拟赛连最长下降子序列都不会写,心想要是T3是dp就咕咕咕了.去年那 ...

  10. codevs——2147 数星星

    2147 数星星  时间限制: 3 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 小明是一名天文爱好者,他喜欢晚上看星星 ...