Text Codec for Binary and N-ary Trees

Asked at Uber · Coding Questions · Difficulty: Hard · Last asked Jun 17, 2026

Trees often need to be saved to a file or sent over a network as plain text, then rebuilt exactly on the other side. Write a codec that turns a binary tree into a string and rebuilds the same tree from that string. A follow-up asks for the same for an N-ary tree, where each node can have any number of children.

Loading the problem…