#449

Serialize and Deserialize BST

specialist · 720 · lc medium +30 · 59.4% accepted · 3,590 likes · top 57%

play →

Description

Implement a pair of functions that encode a binary search tree to a string and decode the string back to the original BST. Any encoding format is acceptable as long as the round-trip is lossless. Strive for the most compact representation possible.

Example 1:

Input: root = [2,1,3]
Output: [2,1,3]

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