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

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/recover-binary-search-tree/description/

题目描述:

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?

题目大意

删除二叉树中指定一节点,并调整二叉树,使得结果的二叉树仍然满足BST的条件。

解题方法

同学们,看到BST就想什么?对,中序遍历是有序的。

那么,如果其中两个被交换了,那么中序遍历的结果一定也就不对了。比如:

[1, 2, 3, 4, 5, 6]  ==>  [1, 5, 3, 4, 2, 6]

那么,可以看出5这个数字比后面的3大,说明他被打乱了;另外2这个数字,比前面的数字4小,所以他也被打乱了。

所以,可以通过先进行中序遍历得到所有的,然后再查找哪些乱了,再复原,时间复杂度O(n)。

但是,中序遍历的操作不需要完全完成。在中序遍历的过程中,用一个指针保存上个节点,那么当前节点值应该小于前一个节点的值。否则就存在乱序。

第一个乱序的数字是pre,第二个乱序的数字是root,所以用两个指针分别保存。

代码:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def recoverTree(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
self.pre, self.first, self.second = None, None, None
self.inOrder(root)
self.first.val, self.second.val = self.second.val, self.first.val def inOrder(self, root):
if not root: return
self.inOrder(root.left)
if self.pre and self.pre.val > root.val:
if not self.first:
self.first = self.pre
self.second = root
self.pre = root
self.inOrder(root.right)

日期

2018 年 3 月 23 日 ———— 科目一考了100分哈哈哈哈~嗝~

【LeetCode】99. Recover Binary Search Tree 解题报告(Python)的更多相关文章

  1. LeetCode: Recover Binary Search Tree 解题报告

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

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

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

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

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

  4. leetcode 99 Recover Binary Search Tree ----- java

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

  5. [leetcode]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

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

  7. 第五周 Leetcode 99. Recover Binary Search Tree (HARD)

    Leetcode99 给定一个 二叉搜索树,其中两个节点被交换,写一个程序恢复这颗BST. 只想到了时间复杂度O(n)空间复杂度O(h) h为树高的解法,还没想到空间O(1)的解法. 交换的情况只有两 ...

  8. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

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

随机推荐

  1. 变量、内存区域、MDK文件(map、htm)

    变量分为:局部变量和全局变量 局部变量:函数体内部定义的变量,作用域为函数内部,static声明(静态局部变量)该变量则函数调用结束后不消失而保留值,分配的存储空间不释放. 全局变量:函数体外部定义的 ...

  2. 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享三:问题2

    框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 搜索框是该项目重要的一环,由于涉及 ...

  3. RocketMQ这样做,压测后性能提高30%

    从官方这边获悉,RocketMQ在4.9.1版本中对消息发送进行了大量的优化,性能提升十分显著,接下来请跟着我一起来欣赏大神们的杰作. 根据RocketMQ4.9.1的更新日志,我们从中提取到关于消息 ...

  4. C++中的字符串输入getline

    http://www.cnblogs.com/wanghao111/archive/2009/09/05/1560822.html 1 #include <iostream> 2 #inc ...

  5. A Child's History of England.12

    Dunstan, Abbot of Glastonbury Abbey, was one of the most sagacious of these monks. He was an ingenio ...

  6. 实时数仓(二):DWD层-数据处理

    目录 实时数仓(二):DWD层-数据处理 1.数据源 2.用户行为日志 2.1开发环境搭建 1)包结构 2)pom.xml 3)MykafkaUtil.java 4)log4j.properties ...

  7. 虚拟机中安装centos系统的详细过程

    linux-centos的安装 检查电脑是否开启虚拟化,只有开启虚拟化才能安装虚拟机 新建虚拟机 鼠标点进去,选中红框所示,回车 登录: 输入默认用户名(超级管理员 root) 密码:安装时设置的密码

  8. eclipse上点击open Perspective找不到java EE的解决办法

    原因:没有安装java ee等插件 Help--->Install New software---->work  with中选择All Available  Sites---->  ...

  9. 13个酷炫的JavaScript一行程序

    1. 获得一个随机的布尔值(true/false) const randomBoolean = () => Math.random() >= 0.5; console.log(random ...

  10. jenkins+Gitlab安装及初步使用

    安装包下载地址:https://packages.gitlab.com/gitlab/gitlab gitlab-cerpm 包国内下载地址: https://mirrors.tuna.tsinghu ...