MemotivaLeetCode Patterns Flashcards: Trees, DFS, BFS, Traversals, BST Properties

How do you serialize and deserialize a binary tree?

LeetCode Patterns Flashcards: Trees, DFS, BFS, Traversals, BST Properties

Аудио-карточка · 0:32

Nortren·

How do you serialize and deserialize a binary tree?

0:32

Serialization converts a tree into a string representation, and deserialization reconstructs the tree from the string. Use preorder DFS: record each node's value, and use a special marker like "null" for null children. The string becomes a comma-separated sequence of values and null markers. To deserialize, iterate through the values: each value creates a node, recursively build its left then right children, and null markers return null to stop recursion. The preorder traversal with null markers captures the complete tree structure because the null markers encode where subtrees end. ---
neetcode.io