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.

Sol:

Serialize:

Use preorder to keep the BST data structure and store data in a list as strings.

Deserialize:

Pop elements from the serialization list and biuld new nodes in a preorder manner.

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Codec: # Time O(n)
def serialize(self, root):
"""Encodes a tree to a single string. :type root: TreeNode
:rtype: str
"""
vals = [] def preOrder(node):
if node:
vals.append(node.val)
preOrder(node.left)
preOrder(node.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()
node = TreeNode(val)
node.left = build(minVal, val)
node.right = build(val, maxVal)
return node return build(float('-INF'), float('INF')) # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))

449. Serialize and Deserialize BST的更多相关文章

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

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

  2. [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 ...

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

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

  4. 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 ...

  5. LeetCode 449. Serialize and Deserialize BST

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

  6. 【leetcode】449. Serialize and Deserialize BST

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

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

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

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

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

  9. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

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

随机推荐

  1. Idea单元测试Junit Generator设置

    0. setting--->plugins--->brose repostories-->输入JUnitGenerator V2.0 1.junit generator自动生成测试代 ...

  2. js实现简单拖拽效果

    方法如下: var params = { left: 0, top: 0, currentX: 0, currentY: 0, flag: false }; var getCss = function ...

  3. pta l2-13(红色警报)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805063963230208 题意:给n个顶点,m条边,问每次删 ...

  4. UVM1

    1.UVM: p_sequencer 的使用 http://blog.csdn.net/tingtang13/article/details/46546395

  5. 分组背包模板题 hdu1712

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 第一次接触分组背包,参考博客:https://blog.csdn.net/yu121380/ar ...

  6. LibreOJ 6281 数列分块入门5

    题目链接:https://loj.ac/problem/6281 参考博客:https://blog.csdn.net/qq_36038511/article/details/79725027 我一开 ...

  7. java中替换${xx}

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class replace { public static ...

  8. PAT L2-011 玩转二叉树(二叉树层序遍历)

    给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出 ...

  9. 《centos系列》ubuntu终端链接centos服务器

    首先你得知道centos的账户密码:如果你不知道可以直接在centos下使用root用户: passwd 用户名 直接更新用户的密码. 然后在ubuntu终端(前提是已经安装了ssh): ssh ad ...

  10. 【linux】常见问题&常用命令笔记

    1.重启以及关机命令: Linux centos重启命令: (1)reboot   普通重启 (2)shutdown -r now 立刻重启(root用户使用) (3)shutdown -r 10 过 ...