#297

Serialize and Deserialize Binary Tree

expert · 1170 · lc hard +32 · 60.4% accepted · 11,100 likes · top 59%

play →

Description

Serialization converts a data structure into a format suitable for storage or transmission, after which it can be reconstructed in the same or another environment.

Design an algorithm to serialize a binary tree to a string and deserialize that string back to the original tree structure. The encoding format is entirely your choice — correctness of the round-trip is the only requirement.

Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Example 1:

Input: root = [1,2,3,null,null,4,5]
Output: [1,2,3,null,null,4,5]

Example 2:

Input: root = []
Output: []

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29