原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/

题意:

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

解题思路:这题是说一颗二叉查找树中的某两个节点被错误的交换了,需要恢复成原来的正确的二叉查找树。

算法一:思路很简单,一颗二叉查找树的中序遍历应该是升序的,而两个节点被交换了,那么对这个错误的二叉查找树中序遍历,肯定不是升序的。那我们只需把顺序恢复过来然后进行重新赋值就可以了。开辟两个列表,list用来存储被破坏的二叉查找树的节点值,listp用来存储二叉查找树的节点的指针。然后将list排序,再使用listp里面存储的节点指针赋值就可以了。

代码:

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a tree node
def inorder(self, root, list, listp):
if root:
self.inorder(root.left, list, listp)
list.append(root.val); listp.append(root)
self.inorder(root.right, list, listp)
def recoverTree(self, root):
list = []; listp = []
self.inorder(root, list, listp)
list.sort()
for i in range(len(list)):
listp[i].val = list[i]
return root

算法二:

题目有一个附加要求就是要求空间复杂度为常数空间。而算法一的空间复杂度为O(N),还不够省空间。以下的解法也是中序遍历的写法,只是非常巧妙,使用了一个prev指针。例如一颗被破坏的二叉查找树如下:

        4

       /     \

              2        6

/   \    /   \

1    5  3    7

很明显3和5颠倒了。那么在中序遍历时:当碰到第一个逆序时:为5->4,那么将n1指向5,n2指向4,注意,此时n1已经确定下来了。然后prev和root一直向后遍历,直到碰到第二个逆序时:4->3,此时将n2指向3,那么n1和n2都已经确定,只需要交换节点的值即可。prev指针用来比较中序遍历中相邻两个值的大小关系,很巧妙。

代码:

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a tree node
def FindTwoNodes(self, root):
if root:
self.FindTwoNodes(root.left)
if self.prev and self.prev.val > root.val:
self.n2 = root
if self.n1 == None: self.n1 = self.prev
self.prev = root
self.FindTwoNodes(root.right)
def recoverTree(self, root):
self.n1 = self.n2 = None
self.prev = None
self.FindTwoNodes(root)
self.n1.val, self.n2.val = self.n2.val, self.n1.val
return root

[leetcode]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] Recover Binary Search Tree 复原二叉搜索树

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

  3. [Leetcode] Recover Binary Search Tree

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

  4. LeetCode: Recover Binary Search Tree [099]

    [题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without cha ...

  5. [Leetcode] Recover binary search tree 恢复二叉搜索树

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

  6. LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误

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

  7. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...

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

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

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

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

随机推荐

  1. Android - Bottom Navigation View

    目录 Android - Bottom Navigation View Android - Bottom Navigation View Overview 一直以来,关于Android的底部导航的功能 ...

  2. 数据包编辑工具bittwiste

    数据包编辑工具bittwiste   bittwiste是数据包重放工具bittwist的一个工具.该工具可以编辑修改PCAP抓包文件.该工具提供数据包过滤功能,如根据范围和时间过滤.同时,该工具支持 ...

  3. android 滑动冲突

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 通过move事件的 拦截. 在滑动组件中,重写 在拦截触摸事件的时候  这个方法, 然后 ...

  4. BZOJ.1085.[SCOI2005]骑士精神(迭代加深搜索)

    题目链接 最小步数这类,适合用迭代加深搜索. 用空格走代替骑士. 搜索时记录上一步防止来回走. 不需要每次判断是否都在位置,可以计算出不在对应位置的骑士有多少个.而且每次复原一个骑士至少需要一步. 空 ...

  5. Linux学习笔记02—磁盘分区

    下面介绍四种最常见的分区方式: (1)    最简单的分区方案. SWAP分区:即交换分区,建议大小是物理内存的1-2倍. /分区:建议大小在6GB以上. 使用以上的分区方案,所有的数据都在/分区上, ...

  6. Mac安装jdk1.6

    需到apple官网下载 下载地址:https://support.apple.com/kb/DL1572?viewlocale=en_US&locale=en_US 相关介绍: http:// ...

  7. 接口开发-集成数据库操作(mybatis)

    关于数据存储,最常用的方式就是存到数据库,此篇以MySQL数据库为例,以mybatis框架完成数据库的操作. 一.添加对应依赖 <!-- 数据库:MySQL --> <depende ...

  8. Jenkins部署在Tomcat容器提示:反向代理设置有误

    这个错误我研究了一下,应该是部署在Tomcat时特有的错误,并且我把启动的目录设置成ROOT同样也还在,网上说在系统设置里面的Jenkins URL设置后可以解决,但我测试了同样不行. 如果使用jav ...

  9. 关于eclipse的indigo版中文注释时字体太小的问题(转)

    eclipse目前最新版代号indigo, 在win7上使用时中文注释时字体太小的问题. 为什么会这样? 首先我们应该知道, 在win7系统中, font是有"显示"和" ...

  10. sqlserver 2012 IDE中 Windows身份验证连接服务器报错 ,Login failed for user 'xxx\Administrator'. 原因: 找不到与提供的名称匹配的登录名。

    问题描述: 本地装了两个实例,一个是SQLEXPRESS,可以正常操作.但是另一个开发常用的实例MSSQLSERVER却连Windows身份验证都报错,报的错误也是很奇葩,怎么会找不到Administ ...