Tree DFS
LC #297Hard
Serialize and Deserialize Binary Tree
Tree DFS
MetaGoogleAmazonMicrosoftBloombergProblem
Design an algorithm to serialize a binary tree to a string and deserialize it back to the original tree.
treedfsdesignstring
Constraints
- ›0 ≤ number of nodes ≤ 10⁴
- ›-1000 ≤ node.val ≤ 1000
Example
Input
root = [1, 2, 3, null, null, 4, 5]Output
"1,2,3,null,null,4,5" → reconstructed treeWhy
BFS level-order encoding with null markers allows exact reconstruction