algorythms
Tree DFS
LC #297Hard

Serialize and Deserialize Binary Tree

Tree DFS
MetaGoogleAmazonMicrosoftBloomberg

Problem

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

Inputroot = [1, 2, 3, null, null, 4, 5]
Output"1,2,3,null,null,4,5" → reconstructed tree
Why

BFS level-order encoding with null markers allows exact reconstruction

Hints — reveal one at a time