【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

标签: LeetCode


题目地址:https://leetcode.com/problems/serialize-and-deserialize-bst/description/

题目描述:

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.

The encoded string should be as compact as possible.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

题目大意

用字符串编码一个BST,并实现解编码成BST的函数。

解题方法

看到BST,就一定想到了一个性质: BST的中序遍历是有序的。但我们又知道,只知道树的一种遍历方式,是没法确定这个树的,BST也不例外。

因此,这个题采用前序遍历的方式,这样,遍历得到的第一个数组就是BST的根节点,数组后面的这些数中比根节点的值小的是根节点的左子树,比根节点值大的是根节点的右子树(BST的最重要性质)。

因此,重要结论:BST的前序遍历能唯一的确定一颗BST

解编码过程是通过一个队列进行操作。其实也可以是list,不过队列的效率更高。

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Codec: def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
vals = []
def preOrder(root):
if root:
vals.append(root.val)
preOrder(root.left)
preOrder(root.right)
preOrder(root)
return ' '.join(map(str, vals)) def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
vals = collections.deque(int(val) for val in data.split())
def build(minVal, maxVal):
if vals and minVal < vals[0] < maxVal:
val = vals.popleft()
root = TreeNode(val)
root.left = build(minVal, val)
root.right = build(val, maxVal)
return root
return build(float('-inf'), float('inf')) # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))

日期

2018 年 3 月 12 日

【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)的更多相关文章

  1. [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  2. [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. LeetCode 449. Serialize and Deserialize BST

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...

  4. [leetcode]449. Serialize and Deserialize BST设计BST的编解码

    这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...

  5. 【leetcode】449. Serialize and Deserialize BST

    题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...

  6. 449. Serialize and Deserialize BST

    https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...

  7. 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树

    详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...

  9. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

随机推荐

  1. 20-Integer to Roman-Leetcode

    比较简单的思路:用map存放各个位的数字到罗马字符的映射 然后从个位依次遍历高位加上映射即可. Given an integer, convert it to a roman numeral. Inp ...

  2. Redis的六种特性 场景

    Redis的六种特性 l Strings l Hashs l Lists l Sets l Sorted Sets l Pub/Sub Redis各特性的应用场景 Strings Strings 数据 ...

  3. 17.Power of Four-Leetcode

    #define IMIN numeric_limits<int>::min() #define IMAX numeric_limits<int>::max() class So ...

  4. Python查找最长回文暴力方法

    查找最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 例如1: 输入: "babad" 输出: "bab" ...

  5. 巩固javaweb第十六天

    巩固内容: 下拉框 在注册功能中,地区的选择使用了下拉框,可以从地区选项中选择一个地区.在这个 例子中,只允许选择一个,而在有些情况下,下拉框可以进行多选.所以,从功能上来说, 下拉框具有单选按钮和复 ...

  6. Applescript快速入门及OmniFocus每日md报告开发

    本篇主要记录 Applescript 基础语法,以及利用 applescript 生成 omnifocus 每日报告 从 windows 转换到 macos,最近一直在不断折腾,这两天浏览 githu ...

  7. A Child's History of England.19

    The King was at first as blind and stubborn as kings usually have been whensoever [每当] they have bee ...

  8. A Child's History of England.30

    CHAPTER 10 ENGLAND UNDER HENRY THE FIRST, CALLED FINE-SCHOLAR Fine-scholar, on hearing of the Red Ki ...

  9. Spark(十六)【SparkStreaming基本使用】

    目录 一. SparkStreaming简介 1. 相关术语 2. SparkStreaming概念 3. SparkStreaming架构 4. 背压机制 二. Dstream入门 1. WordC ...

  10. C/C++ Qt 数据库与SqlTableModel组件应用

    SqlTableModel 组件可以将数据库中的特定字段动态显示在TableView表格组件中,通常设置QSqlTableModel类的变量作为数据模型后就可以显示数据表内容,界面组件中则通过QDat ...