Serialize And Deserialize Binary Tree
Implement serialize(root) producing a string and deserialize(data) reconstructing a binary tree with identical structure and values. Use any complete unambiguous format. The judge tests round trips, including empty trees.
Example 1
root = [1,2,3,null,4][1,2,3,null,4]Explanation
Preserve the null left child below 2 so 4 returns as its right child.
Example 2
root = []nullExplanation
The empty tree must not become a leaf during round-trip.
Example 3
root = [-2,null,5][-2,null,5]Explanation
Preserve the negative root, missing left child and right child 5.
Constraints
- 0 <= number of nodes <= 10000
- Values are signed 32-bit integers
- deserialize receives a serialization produced by your encoder
Function contract
serializeArguments: treeSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 34 cases, on your device.
[1,2,3,null,4]