21 · Tree Traversals

#297 Serialize and Deserialize Binary Tree

Hardsolve on LeetCode ↗

frequently reported atLinkedInMicrosoftByteDance / TikTokUberAmazonNVIDIA

the problem

In plain words

Design a Codec with serialize(root) → String and deserialize(data) → root, such that deserialize(serialize(tree)) rebuilds the same tree. One Codec object may write and a different one read. The format is up to you.

in root = [1,2,3,null,null,4,5]

out [1,2,3,null,null,4,5]

Any string format works as long as the round trip is exact.

in root = []

out []

  • 0 ≤ number of nodes ≤ 10⁴
  • -1000 ≤ Node.val ≤ 1000

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #105
    2. #102
    3. #100
      Same Tree
      Easyin Tree Recursion