题目链接:Recover Binary Search Tree | LeetCode OJ

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?

Tags: Tree, Depth-first Search

分析

这道题的基本要求是使用O(n)的空间,进阶要求是使用常数空间。O(n)的算法比较直接,直接从二叉查找树的用途就能推出。二叉查找树的特点是中序遍历后能够生成递增的序列,因此只需要对给定的二叉查找树进行中序遍历,遍历过程中找到非递增情况,就能够得出不符合递增规律的两个数,交换后二叉查找树的恢复就完成了。

在设计使用常数空间的算法时,一种思路是在中序遍历中传递上一个遍历到的值,这样就在遍历过程中完成了比对,而不需额外的空间存储遍历结果,但这种算法本质也是O(n)的,因为普通的深度优先遍历中用到的递归,本质上是想中间结果压入栈内,因此还是需要O(n)的空间复杂度。

真正O(n)空间复杂度的算法需要用时间换空间,常用的算法是Morris二叉树遍历算法。

示例

class Solution:
_previous = None
_swapA = _swapB = None # @param root, a tree node
# @return a tree node
def recoverTree(self, root):
self._coverTree(root) if self._swapA is not None and self._swapB is not None:
temp = self._swapA.val
self._swapA.val = self._swapB.val
self._swapB.val = temp
return root def _coverTree(self, root):
if root is None or root.val is None:
return self._coverTree(root.left)
if self._previous is not None and self._previous.val is not None:
if self._previous.val > root.val:
if self._swapA is None:
self._swapA = self._previous
self._swapB = root self._previous = root
self._coverTree(root.right)

Leetcode 笔记系列的Python代码共享在https://github.com/wizcabbit/leetcode.solution

示例说明

  • 使用中序遍历时,最直接的想法是中序遍历,同时将遍历结果储存在一个数组中。中序遍历结束后,再行遍历这个数组查找非递增的值。其实还可以在中序遍历过程中每次传入上一个结点的值,一边遍历一边比对是否递增。当然这种方法的实际空间复杂度没有变化,因为递归过程中的每次中间结果都会被压栈,实际仍然使用了和单独数组同样的空间

相关题目

Validate Binary Search Tree

Leetcode 笔记 99 - Recover Binary Search Tree的更多相关文章

  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 Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

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

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

  4. 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]

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

  5. LeetCode OJ 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(复原BST) ☆☆☆☆☆

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

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

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

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

  9. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

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

随机推荐

  1. ASP.NET Core的路由[5]:内联路由约束的检验

    当某个请求能够被成功路由的前提是它满足某个Route对象设置的路由规则,具体来说,当前请求的URL不仅需要满足路由模板体现的路径模式,请求还需要满足Route对象的所有约束.路由系统采用IRouteC ...

  2. OVS 中的各种网络设备 - 每天5分钟玩转 OpenStack(128)

    上一节我们启用了 Open vSwitch,本节将查看当前的网络状态并介绍 Open vSwitch 涉及的各种网络设备 初始网络状态 查看一下当前的网络状态. 控制节点 ifconfig 显示控制节 ...

  3. 使用 Android Studio 检测内存泄漏与解决内存泄漏问题

    本文在腾讯技术推文上 修改 发布. http://wetest.qq.com/lab/view/63.html?from=ads_test2_qqtips&sessionUserType=BF ...

  4. 【C#附源码】数据库文档生成工具支持(Excel+Html)

    [2015] 很多时候,我们在生成数据库文档时,使用某些工具,可效果总不理想,不是内容不详细,就是表现效果一般般.很多还是word.html的.看着真是别扭.本人习惯用Excel,所以闲暇时,就简单的 ...

  5. 玩转spring boot——开篇

    很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...

  6. 【微信小程序开发•系列文章六】生命周期和路由

    这篇文章理论的知识比较多一些,都是个人观点,描述有失妥当的地方希望读者指出. [微信小程序开发•系列文章一]入门 [微信小程序开发•系列文章二]视图层 [微信小程序开发•系列文章三]数据层 [微信小程 ...

  7. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  8. git远程库GitHub

    首先,注册一个GitHub(github.com)帐号,免费获得Git远程仓库 由于本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以,需要一点设置: 第1步:创建SSH Key.在用 ...

  9. postgresql 基本语法

    postgresql数据库创建/修改/删除等写入类代码语法总结: 1,创建库 2,创建/删除表 2.1 创建表 create table myTableName 2.2 如果表不存在则创建表 crea ...

  10. 如何查看w3p.exe 和IIS 应用程序池的关系

    图形界面方式 命令行方式 如果找不到 appcmd Appcmd.exe exists at the location %systemroot%\system32\inetsrv\. You eith ...