← 返回 uber 的题目列表Serialize / Deserialize Binary Tree with N-ary Follow-up
类型:qbank
Senior tech phone-screen prompt. Implement serialization and deserialization for a binary tree, then extend the scheme to an N-ary tree. The interviewer asks the candidate to invent test cases and run code.
Requirements
Implement a serializer that converts a binary tree into a string or sequence representation.
Implement a deserializer that reconstructs the original binary tree from that representation.
The round expects runnable code and candidate-created tests.
Follow-up: extend the same idea to an N-ary tree.
The representation must preserve enough null / child-boundary information to reconstruct shape, not only values.
Notes
Clarify node value constraints and whether duplicate values are allowed; serialization must not rely on uniqueness.
For the N-ary follow-up, the key is encoding child counts or explicit delimiters so the parser knows where each node's child list ends.
Bring your own tests: empty tree, single node, asymmetric tree, duplicate values, and an N-ary node with zero / one / many children.
Preparation
Practice preorder-with-null-markers and level-order-with-null-markers for binary trees.
Write one N-ary serializer using (value, child_count) records and one using bracket delimiters.
Rehearse explaining why values alone cannot reconstruct tree shape.