Skip to content
Veltoryx Technologies
ProProblem 105● MediumLeetCode #449 ↗4 min readUpdated 30 October 2026

Serialize and Deserialize BST

Compactly serialize and deserialize a binary search tree using preorder without null markers.

01The problem

Serialization is converting a data structure into a sequence of bits so that it can be stored in a file or memory buffer. Deserialization reconstructs the original tree structure from the string.

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: do not use null markers!

text
       5
      /      3   6
    / \      2   4   7

Serialized: "5 3 2 4 6 7"
Deserialized: original tree restored identically

Constraints

  • The number of nodes in the tree is in the range [0, 10^4].
  • 0 <= Node.val <= 10^4
  • The input tree is guaranteed to be a valid BST.

Loading the rest of the lesson…