题目

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?

分析

给定一颗二叉排序树,它的两个节点被交换,要求在常数空间复杂度内把它恢复为原来的二叉排序树。

方法一:我们知道二叉排序树的中序遍历序列为递增的有序序列,首先,中序遍历该二叉树,存储元素序列,然后循环查找元素序列中逆序的两个节点元素,交换之,还原成二叉树即可。该方法需要O(n)的空间,n为节点个数,不满足要求。

查找资料,得到方法二:

递归中序遍历二叉树,设置一个pre指针,记录当前节点中序遍历时的前节点,如果当前节点大于pre节点的值,说明需要调整次序。

有一个技巧是如果遍历整个序列过程中只出现了一次次序错误,说明就是这两个相邻节点需要被交换。如果出现了两次次序错误,那就需要交换这两个节点。

AC代码

/**
* 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: //节点p,q为两个逆序节点 , pre为遍历当前root节点的前驱
TreeNode *p = NULL, *q = NULL, *pre = NULL; void recoverTree(TreeNode* root) {
if (!root)
return;
//中序遍历二叉树,查找需要逆序节点
InOrder(root); if (p && q)
{
int tmp = p->val;
p->val = q->val;
q->val = tmp;
}//if
} //中序遍历二叉树root,同时查找树中逆序节点p , q
void InOrder(TreeNode *root)
{
if (!root)
return;
if (root->left)
InOrder(root->left);
if (pre != NULL && pre->val > root->val)
{
if (!p)
p = pre; q = root;
}//if
pre = root;
if (root->right)
InOrder(root->right);
} };

GitHub测试程序源码

LeetCode(99) Recover Binary Search Tree的更多相关文章

  1. LeetCode(98) Validate Binary Search Tree

    题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...

  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(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  4. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

  5. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

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

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

  9. 【LeetCode】99. Recover Binary Search Tree

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

随机推荐

  1. [洛谷P1434] [SHOI2007]滑雪

    题目链接: here we go 题外话: 谁能想到这是一道咕了两年的\(AC\)呢--当年是在搜索还半懂不懂的时候遇到的这道题,感觉真是难得要命()所以一直拖着不做,后面就下意识地逃避了搜索相关的内 ...

  2. java中代码执行顺序

    静态代码块 -- >构造代码块 --> 构造方法静态代码块:只执行一次构造代码块:每次调用构造方法都执行 http://blog.csdn.net/wuhaiwei002/article/ ...

  3. python入门之实例-用户登录、注册

    用户密码存储文件db(其中用户和密码之间用$符合隔开): admin$123456 root$sdfk9f24 chy$654321 代码如下: def login(username,password ...

  4. python转换已转义的字符串

    python转换已转义的字符串 有时我们可能会获取得以下这样的字符串: >>> a = '{\\"name\\":\\"michael\\"} ...

  5. 安卓新的联网方式 Volley的使用(一)加载图片与 json

    最近刚接触安卓, 以前搞wp ,一对比起来 ,安卓怎么这么麻烦.联网必须要重新开一个线程才可以.而且加载网络图片也很麻烦...花了很久一直卡在快速滑动加载网络图片的listview上面 ,一直很纠结痛 ...

  6. 服务器 未能加载文件或程序集“XXXX”或它的某一个依赖项。试图加载格式不正确的程序。

    ,本人采用的第一种解决办法解决,已解决 问题2: 在同一个服务器上想要一个IP有两个网址,配置端口号,给新端口号开权限

  7. React 实践记录 02 Flux introduction

    Introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出. 一旦稍微多了解一点React,很难避免听到Flux这个名词. Flux是一 ...

  8. 【NumPy学习指南】day5 改变数组的维度

    我们已经学习了怎样使用reshape函数,现在来学习一下怎样将数组展平. (1) ravel 我们可以用ravel函数完成展平的操作: In: b Out: array([[[ 0, 1, 2, 3] ...

  9. Ubuntu下软件的搜索与安装

    本文为笔者原创,首发于简书(点击这里查看). 小白玩转linux的第一个拦路虎就是软件的安装了.本文结合自己在Ubuntu14.04下软件安装经验做一个总结. 1.如何搜索软件? apt-cache ...

  10. vba 时间

    Sub tt1() Dim d1, d2 As Date d1 = #//# d2 = #//# Debug.Print "相隔" & (d2 - d1) & &q ...