449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/
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,要求生成的字符串尽可能短。序列化普通二叉树需要特殊字符表示null来区分左右子树,BST可利用它自身的性质在preorder遍历时知道遍历到的是左树还是右树从而省去null:值小于parent时说明还在左树,大于时说明在右树。为了进一步压缩空间,将4byte的整数值每一个byte转成一个char,一个结点的值固定对应字符串里的4个char。
空间压缩参考了这。
Last updated
Was this helpful?