An interactive visualization of preorder DFS serialization and deserialization. Watch the tree convert to a string representation and back, node by node.
DFS LeetCode 297 โ Hard[1,2,3,null,null,4,5] serializes to "1,2,null,null,3,4,null,null,5,null,null"
using preorder DFS with null markers.
Preorder traversal (root, left, right) with null markers uniquely identifies a binary tree.
By recording "null" for every missing child, we capture the full structure โ not just the values.
During deserialization, we consume tokens in the exact same preorder sequence, using "null" tokens
to know when a subtree is empty and we should backtrack.
null child, append the string "null".i that tracks which token to consume next.null), then recurse for left and right children.
Both serialization and deserialization visit each node exactly once. The serialized string and
the reconstructed tree each use O(n) space, where n is the number of nodes.
The recursion stack uses O(h) space where h is the height of the tree (O(n) in the worst case for a skewed tree).