题目描述:

由于某种原因一个二叉排序树的两个节点的元素被交换,在不改变树的结构的情况下恢复这颗二叉排序树

题目来源:

http://oj.leetcode.com/problems/recover-binary-search-tree/

题目分析:

中序遍历二叉排序树会得到递增序列,如果两个元素交换,递增序列的顺序必被破坏,例如:1, 2, 3, 4, 5, 6, 7。交换后可能会有

(1)1, 2, , 4, 5, , 7 (2)1, 2, 3, 4, , ,7 等

在中序遍历时,出现当前元素比中序遍历中前一个元素小时,找到出错元素,维护一个刚刚遍历的节点的指针pre,当遍历结束当前节点时,更新pre指针

时间复杂度:O(1)

示例代码:
TreeNode *pre, *first, *second;

void recoverTree(TreeNode *root) {
pre = first = second = NULL;
inOrder(root);
int tmp = second->val;
second->val = first->val;
first->val = tmp;
} void inOrder(TreeNode*& root) {
if(root == NULL)
return ; inOrder(root->left);
if(pre != NULL && pre->val > root->val) {
first == NULL ? first = pre, second = root : second = root;
}
pre = root;
inOrder(root->right); return ;
}

Recover Binary Search Tree-恢复二叉查找树的更多相关文章

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

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

  2. [Leetcode] 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

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

  4. 【leetcode】Recover Binary Search Tree

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

  5. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

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

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

  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] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆

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

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

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

  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. [Android Training视频系列] 8.2 Managing Audio Focus

    视频讲解:http://www.eyeandroid.com/thread-15896-1-1.html 由于很多应用程序都可以播放音频,因此在播放前考虑它们如何交互就显得很重要了,为了避免同时出现多 ...

  2. DebugViewHierarchy

    DebugViewHierarchy(视图调试)是XCode6新出的一项功能,它可以让开发者在程序运行时,动态的查看当前界面的显示情况,包括视图的层次,控件的大小和位置,而且会以3D效果显示当前视图的 ...

  3. c读取文本文档

    想数一下文本文档一共有多少行,写了个小程序 1.用fopen()以只读方式打开文件 2.用fgetc()获取文件流中的字符内容 3.如果字符内容为'\n'换行符,count++ 最后输出count的值 ...

  4. Java_Swing实现小球沿正弦曲线运动的代码

    1 package zuidaimapack_1; import java.awt.*; import javax.swing.*; /** *Java_Swing实现小球沿正弦曲线运动的代码 * @ ...

  5. 用Chrome devTools 调试Android手机app中的web页面。

    (1) 手机要满足Android系统为4.4或更高版本,低版本不支持这种方式.(2) 确保App已经开启了webview的debug调试模式,由Android工程师协助.(2) 用usb数据线连接好手 ...

  6. 从URL中获取搜索关键字

    public string GetSearchKeyWords(string strQuery) { string result = ""; string pattern = &q ...

  7. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  8. HDU 5597 GTW likes function 欧拉函数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5597 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  9. 01.安装Memcached

    1.安装Memcached 1.下载Memcached及其依赖 下载memcached-1.4.24.tar.gz和libevent-2.0.22-stable.tar.gz文件并解压如下: [liz ...

  10. Ubuntu 下编译安装linux

    1. 准备工作切换为管理员权限,sudo –i 输入用户密码 进入root 权限apt-get install build-essential kernel-package libncurses5-d ...