Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Serialize and Deserialize BST (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

serialization means converting a data structure or object into a sequence of bits that can be stored or sent over a network. Deserialization is the reverse process, which takes this sequence and recreates the original object or data structure.

Design an algorithm to serialize and deserialize a binary search tree (BST).

The serialization should convert the BST into a string, and deserialization should reconstruct the original BST from this string. There is no restriction on how your serialization/deserialization algorithm should work.

Examples

Example 1:

  • Input: root = [4, 2, 6, 1, 3, null, 7]
Image
  • Expected Output: [4, 2, 6, 1, 3, null, 7]
  • Justification: The output is the same as the input.

Example 2:

  • Input: root = [8, 5, 10, 2, null, null, 12]
Image
  • Expected Output: [8, 5, 10, 2, null, null, 12]
  • Justification: The output is the same as the input.

Example 3:

  • Input: root = [15, 10, null, 5, 12, null, null, null, 13]
Image
  • Expected Output: [15, 10, null, 5, 12, null, null, null, 13]
  • Justification: The output is the same as the input.

Constraints:

  • The number of nodes in the tree is in the range [0, 10<sup>4</sup>].
  • 0 <= Node.val <= 10<sup>4</sup>
  • The input tree is guaranteed to be a binary search tree.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible