Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Tree problems in depth

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Tree problems in depth

Serialize and Deserialize Binary Tree

advancedlayer I2 · Tree problems in depth5 tests

Write two functions: serialize(root) turns a binary tree into a string, and deserialize(data) turns that string back into an identical tree.

  • Only the round trip matters — the exact format is yours to choose
  • serialize must return a string; deserialize must return a tree of TreeNodes
  • An empty tree has to survive the trip too
  • Values may be negative, so a format that can tell -3 from a missing child is required

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen