Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

124. Serialize and Deserialize Binary Tree

HardDay 36 of 60Trees & BST

Design a class Codec with two methods: serialize(root) turns a binary tree into a string, and deserialize(data) turns that string back into the exact same tree. You may choose any string format, as long as deserializing a serialized tree gives back an identical tree. The judge checks deserialize(serialize(root)).

Example 1
Input: root = [1,2,3,null,null,4,5]
Output: [1,2,3,null,null,4,5]
For example, serialize could produce "1,2,#,#,3,4,#,#,5,#,#" and deserialize rebuilds the same tree from it.
Example 2
Input: root = []
Output: []
Constraints
  • The number of nodes is in the range 0 to 10^4
  • -1000 <= Node.val <= 1000
Hints
Frequently asked at: Amazon, Meta, Google, Microsoft, LinkedIn, Uber
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for ₹200/ 30 min