题目来源:

  https://leetcode.com/problems/recover-binary-search-tree/


题意分析:

  二叉搜索树中有两个点错了位置,恢复这棵树。


题目思路:

  如果是没有空间复杂度限制还是比较简单。首先将树的值取出来,然后排序,将相应的位置判断是否正确。如果要特定空间复杂度就难很多。


代码(python):

  

# 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.
"""
ans,ansr = [],[]
def solve(root):
if root:
solve(root.left)
ans.append(root.val);ansr.append(root)
solve(root.right)
solve(root)
ans.sort()
for i in range(len(ans)):
ansr[i].val = ans[i]

[LeetCode]题解(python):099-Recover Binary Search Tree的更多相关文章

  1. Java for LeetCode 099 Recover Binary Search Tree

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

  2. LeetCode(99) Recover Binary Search Tree

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

  3. 099 Recover Binary Search Tree 复原二叉搜索树

    二叉排序树中有两个节点被交换了,要求把树恢复成二叉排序树. 详见:https://leetcode.com/problems/recover-binary-search-tree/submission ...

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

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

  5. Leetcode 笔记 99 - Recover Binary Search Tree

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

  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] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

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

  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练习题】Recover Binary Search Tree

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

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

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

随机推荐

  1. HTML5新增加的功能

    1.部分代码代替了以前的代码     例如: 获取焦点     旧:document.getElementById("price");.focus;     新:<input ...

  2. 升级python的sqlite库版本

    今天了解了一下用python获取chrome cookie信息,在研究的过程中,发现打开数据库失败,后来调查了一下发现是由于sqlite3库太老的缘故,起码需要3.8以上,然后看了一下python 2 ...

  3. rsa 密钥和公钥的生成

    openssl genrsa -out prikey.pem openssl rsa -in prikey.pem -pubout -out pubkey.pem

  4. gitlab使用入门

    第一步:安装git软件 方法:百度git 点击下载,然后双击安装,一直点下一步即可.   第二步:设置用户名和邮箱 方法:在桌面上点鼠标右键,选择Git Bash,然后分别运行命令 git confi ...

  5. windows server system32下常见快捷指令

    win+R       命令行窗口 cmd        dos命令窗口 mstsc      远程登录输入窗口 calc         快速打卡计算器 control     打开控制面板 eve ...

  6. windows7下,protel 99se元件库加载问题的解决方案

    方法一:到C盘(系统盘),系统文件夹(c:\windows)下的ADVPCB99SE和ADVSch99SE文件先配置原理图,用本文打开ADVPCB99SE文件,在[Change Library Fil ...

  7. delphi datasnap 心跳包

    为了能让我们的服务程序更加稳定,有些细节问题必须解决.就如上一讲中提到的客户端拔掉网线,造成服务器上TCP变成死连接,如果死连接数量过多,对服务器能长期稳定运行是一个巨大的威胁.另外,经过测试,如果服 ...

  8. Installing perl and writing your first perl program in Ubuntu

    Installing perl and writing your first perl program in Ubuntu     Installing perl and writing your f ...

  9. 多名Uber司机被指刷单遭封号 一周薪水为0

    昨天,一司机在Uber“司机之家”办公地墙上写了泄愤的话 摄/法制晚报记者 苏妮 司机展示的账单显示,上周的薪水几乎为零,上面用英文标注了“欺诈行为”的字样 摄/法制晚报记者 苏妮 法制晚报讯(记者 ...

  10. mongoose的用法(注:连接数据库)

    第一步:连接数据库: mongoose.connect('mongodb://'+user+':'+pass+'@mongo.duapp.com:'+port+'/xzWIRHYlWLAApdsfAz ...