返回题库

二叉树的序列化与反序列化

Serialize and Deserialize Binary Tree

专题
Algorithmic Programming / 算法编程
难度
L4
来源
Citadel

题目详情

问题:二叉树的序列化与反序列化

考察:字符串、树、深度优先搜索

来源:DSA Prep / Citadel

链接:https://leetcode.com/problems/serialize-and-deserialize-binary-tree

英文原题

Problem: Serialize and Deserialize Binary Tree

Patterns: String, Tree, Depth-First Search

Recency: 2yr

Link: https://leetcode.com/problems/serialize-and-deserialize-binary-tree

Source: https://www.dsaprep.dev/blog/citadel-coding-interview-questions/

解析

思路:用前序遍历序列化节点值,并用特殊标记表示空节点。反序列化时按同样前序顺序递归读取,遇到空标记返回 null。

复杂度:序列化和反序列化都是 O(n),空间 O(n)。


英文解析

Approach: Serialize with preorder traversal and an explicit null marker. Deserialization consumes tokens in the same preorder order, recursively rebuilding the node, then its left and right subtrees.

Complexity: Time O(n)O(n), space O(n)O(n).