原题地址: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. ETL数据清洗工具总结

    [国外] 1. datastage点评:最专业的ETL工具,价格不菲,使用难度一般 下载地址:ftp://ftp.seu.edu.cn/Pub/Develop ... taStage.v7.5.1A- ...

  2. ARP协议详解之ARP动态与静态条目的生命周期

    ARP协议详解之ARP动态与静态条目的生命周期 ARP动态条目的生命周期 动态条目随时间推移自动添加和删除. q  每个动态ARP缓存条目默认的生命周期是两分钟.当超过两分钟,该条目会被删掉.所以,生 ...

  3. Nmap扫描教程之Nmap基础知识

    Nmap扫描教程之Nmap基础知识 Nmap扫描Nmap基础知识 Nmap是一个免费开放的网络扫描和嗅探工具包,也叫网络映射器(Network Mapper).Nmap工具可以用来扫描电脑上开放的端口 ...

  4. css实现自适应正方形

    这里介绍7种方法,仅供参考. 1.vm单位 <div class="square-shape">这是一个可以自适应的正方形,此法适用于移动端web页面.</div ...

  5. NCTF2018 Easy_Audit的writeup

    题目直接给出来代码 这题考几个点: 1.$_REQUEST的变量覆盖 2.编码绕过 3.PHP数组特性 4.正则绕过 5.file_get_contents函数 首先一步步把题目分析一遍 if($_R ...

  6. 利用ComponentWillReceiveProps解决异步问题

    1.工作中遇到这么一个问题:有多个按钮,点击不同的按钮发送不同的请求(传的id不同)并显示弹窗,弹窗里要展示后端发送回来的数据.但是比如点击第二个按钮,弹窗里显示的仍然是第一个弹窗里的数据. 2.原因 ...

  7. 华为S5300系列交换机限制特定IP可以登录Web

    针对Web管理可能有如下需求: 1.限制某个特定IP允许访问Web 2.默认修改80端口访问 那么针对上面的设置可以有效杜绝而已Web密码暴力破解,增强交换机安全等. 实现: 1.限制特定IP登录We ...

  8. HDU 3472 HS BDC (混合图的欧拉路径判断)

    HS BDC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. latex编写论文

    写给像我这样需要使用latex编写论文的小菜鸟,给出demo和注释,高级部分自己参透(默认你已经搭好环境). 1.搭论文架子 demo1 \documentclass[10pt,a4paper]{ar ...

  10. stanford CS DB 课程 数据库系统实现

    http://infolab.stanford.edu/db_pages/classes.html   CS145: Introduction to Databases   CS245: Databa ...