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. N分之一 竖式除法模拟

    N分之一 Description Alice越来越痴迷于数学问题了.一天,爸爸出了个数学题想难倒她,让她求1 / n. 可怜的Alice只有一岁零九个月,回答不上来 ~~~~(>_<)~~ ...

  2. vim 编辑器使用法则

    vim 编辑器使用法则 Vi编辑器有3种使用模式:一般模式.编辑模式和命令模式. $SHELL:查看当前默认shell类型  $BASH_VERSION:查看当前shell版本 3.一般模式: 光标移 ...

  3. [java基础原理] BigDecimal

    1.类 简化示例 属于java.math包,因此包含各种数学运算,abs,pow等等. package java.math; public class BigDecimal { //值的绝对long型 ...

  4. Blend 混合

    Shader 中的混合 Blend Off  :不混合 Blend SrcFactor DstFactor  :SrcFactor 是源系数,DstFactor是目标系数,源系数是由片段着色器计算出来 ...

  5. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

  6. Rsync文件同步服务器配置

    rsync 是一个Unix/Linux系统下的文件同步和传输工具.rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法.可以用来做备份或镜像.一.配置文件rsync ...

  7. T1003 电话连线 codevs

    http://codevs.cn/problem/1003/ 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市 ...

  8. IE插件

    在OA上要直接查看word等公告文件,就必须安装office控件.要安装office控件,需要在IE浏览器中做相应的设置.如何设置呢,下面由小编具体介绍下. 工具/原料   OA IE浏览器 方法/步 ...

  9. 我的arcgis培训照片3

    来自:http://www.cioiot.com/successview-557-1.html

  10. Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)

    We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all kn ...