LeetCode 297 Serialize and Deserialize Binary Tree - Solution

The core idea

Use preorder traversal with explicit null markers.

Serialize: Visit root, then left, then right. When you hit a null child, output "null". Separate values with commas.

Deserialize: Read values in order. The first value is the root. Recursively build the left subtree (consuming values until you hit a null), then the right subtree.

The null markers tell you exactly where each subtree ends.