Hard
JAVA-005javaSerialize a Binary Tree
Problem
Serialize a binary tree to a string and deserialize it back. A classic system-design interview question — how would you transmit a tree over a network? Use level-order with `null` sentinels.
Input
Not applicable — demonstrate via a hard-coded tree inside `main`.
Output
Print the serialization, then deserialize and print level-order to verify.
Constraints
Tree nodes up to 10^4, values fit in `int`.
Sample input
(a tree: root=1, L=2, R=3, R.L=4, R.R=5)
Sample output
1,2,3,#,#,4,5 1 2 3 4 5
Explanation
Level-order traversal with `#` for nulls; deserialize reconstructs identical shape.
treebfsdesignserialization@Google@Meta@Bloomberg
Visible test cases
in: (default test tree)
out: 1,2,3,#,#,4,5 1 2 3 4 5
Your solution — run it, use AI if stuck
java
