hard
Serialize and Deserialize Binary Tree
Design two functions: one that converts a binary tree into a string, and one that rebuilds an identical tree from that string. The format is up to you; the pair must round-trip any tree, including ones with null children in arbitrary positions.
Constraints
- 0 ≤ number of nodes ≤ 10^4
- -1000 ≤ node value ≤ 1000
Examples
in: root = [1,2,3,null,null,4,5]
out: serialize → "1,2,#,#,3,4,#,#,5,#,#"; deserialize → the same tree
Code it yourself
Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.