Serialize/Deserialize Binary Tree
Serialize tree to string via BFS with null markers and deserialize back.
Visualization
Interactive visualization for Serialize/Deserialize Binary Tree
Serialize/Deserialize Tree:
- • Convert tree to string & back
- • Preorder traversal
Interactive visualization with step-by-step execution
Implementation
1function serialize(root:any):string{ if(!root) return ""; const q=[root], out:string[]=[]; while(q.length){ const n=q.shift(); if(!n){ out.push("#"); continue;} out.push(String(n.val)); q.push(n.left||null); q.push(n.right||null);} return out.join(','); }
2function deserialize(data:string){ if(!data) return null; const vals=data.split(','); const root:{val:any,left:any,right:any}={val:vals[0],left:null,right:null}; const q=[root]; let i=1; while(q.length && i<vals.length){ const n=q.shift()!; const l=vals[i++]; const r=vals[i++]; if(l!=='#'){ n.left={val:l,left:null,right:null}; q.push(n.left);} if(r!=='#'){ n.right={val:r,right:null,left:null}; q.push(n.right);} } return root; }Deep Dive
Theoretical Foundation
Serialize a binary tree by BFS with null markers so structure and values are preserved; deserialize by reading the stream and reconstructing children level by level.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
Persisting trees to disk
Network transfer of tree data
Coding interview problems (LeetCode 297)
Use Cases
Related Algorithms
AVL Tree (Self-Balancing BST)
A self-balancing binary search tree where the heights of the two child subtrees of any node differ by at most one. Named after inventors Adelson-Velsky and Landis (1962), AVL trees guarantee O(log n) time for search, insert, and delete operations by maintaining balance through rotations. It was the first self-balancing binary search tree to be invented.
Segment Tree (Range Query Tree)
Segment Tree is a binary tree data structure for storing intervals/segments that enables efficient range queries (sum, min, max, GCD) and updates in O(log n) time. Each node represents an interval, with leaves representing single elements. Essential for competitive programming, it solves problems like range sum queries with updates, which would be O(n) with arrays but becomes O(log n) with segment trees.
Fenwick Tree (Binary Indexed Tree - BIT)
A data structure that can efficiently update elements and calculate prefix sums in O(log n) time. Also known as Binary Indexed Tree (BIT), invented by Peter Fenwick in 1994. Fenwick trees are more space-efficient and simpler than segment trees for range sum queries, using only O(n) space compared to segment tree's O(4n).
Lowest Common Ancestor (LCA)
Lowest Common Ancestor (LCA) finds the deepest node that is an ancestor of two given nodes in a tree. This fundamental tree query operation has applications in computational biology (phylogenetic trees), version control systems (finding merge base), network routing, and range queries. Multiple algorithms exist: simple recursive O(n), binary lifting O(log n) per query after O(n log n) preprocessing, and Tarjan's offline algorithm for batch queries.