449.Serialize-and-Deserialize-BST
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.代码
Approach #3 Preorder, 正常顺序
Preorder, 空节点用null=> 1.先访问字符,2. Left, 3. Right,
解析时从0头部开始 => 1.先访问,2. left, 3.right
Approach 1: Postorder traversal 反向顺序
Postorder, 从前往后拼接String => 1.left, 2. Right, 3. Val
解析的时候,从后last往前读取,逆向postorder => 1.访问,2.right, 3. Left
Approach #2 Get rid of delimiters
Last updated
Was this helpful?