Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to serialize and deserialize a binary tree.

Hard

Problem Statement

Write a function to serialize and deserialize a binary tree.

Sample Input

Binary tree: 
 1
 / \ 
 2 3
 / \ 
 4 5

Sample Output

Serialized Tree: 1,2,null,null,3,4,5,null,null,null,null
Deserialized Tree Root: 1

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
class TreeNode { val: number; left: TreeNode
Log in or sign up to run or submit your code.Log inSign up
Input
Binary tree: 
 1
 / \ 
 2 3
 / \ 
 4 5
Expected Output
Serialized Tree: 1,2,null,null,3,4,5,null,null,null,null
Deserialized Tree Root: 1