在计算机科学中,二叉树反序列化是指将一个字符串(通常是通过序列化得到的)重新构建成原始二叉树结构的过程。这是 C++数据结构教程 中非常重要的一个知识点,也是许多算法面试题(如 LeetCode 上的经典题目)的核心内容。
本文将手把手教你如何用 C++ 实现二叉树反序列化,即使你是编程小白,也能轻松理解!
- 序列化:将一棵二叉树转换为一个字符串,便于存储或网络传输。
- 反序列化:将该字符串还原为原来的二叉树结构。
常见的序列化格式有前序遍历、层序遍历等。本文以 层序遍历(BFS) 为基础进行讲解,因为这种方式更直观,也更容易处理空节点(用 "null" 表示)。

首先,我们需要定义二叉树的节点结构:
struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}};假设我们的输入字符串是这样的:"1,2,3,null,null,4,5"。我们需要先按逗号分割,得到一个字符串数组:
#include <vector>#include <string>#include <sstream>#include <queue>std::vector<std::string> split(const std::string& data) { std::vector<std::string> result; std::stringstream ss(data); std::string item; while (getline(ss, item, ',')) { result.push_back(item); } return result;}我们使用 广度优先搜索(BFS) 的思想,借助队列逐层构建节点:
TreeNode* deserialize(const std::string& data) { if (data.empty()) return nullptr; auto nodes = split(data); if (nodes[0] == "null") return nullptr; TreeNode* root = new TreeNode(std::stoi(nodes[0])); std::queue<TreeNode*> q; q.push(root); int i = 1; while (!q.empty() && i < nodes.size()) { TreeNode* node = q.front(); q.pop(); // 处理左子节点 if (nodes[i] != "null") { node->left = new TreeNode(std::stoi(nodes[i])); q.push(node->left); } i++; // 处理右子节点 if (i < nodes.size() && nodes[i] != "null") { node->right = new TreeNode(std::stoi(nodes[i])); q.push(node->right); } i++; } return root;}下面是一个完整的可运行示例:
#include <iostream>#include <vector>#include <string>#include <sstream>#include <queue>struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}};std::vector<std::string> split(const std::string& data) { std::vector<std::string> result; std::stringstream ss(data); std::string item; while (getline(ss, item, ',')) { result.push_back(item); } return result;}TreeNode* deserialize(const std::string& data) { if (data.empty()) return nullptr; auto nodes = split(data); if (nodes[0] == "null") return nullptr; TreeNode* root = new TreeNode(std::stoi(nodes[0])); std::queue<TreeNode*> q; q.push(root); int i = 1; while (!q.empty() && i < nodes.size()) { TreeNode* node = q.front(); q.pop(); if (nodes[i] != "null") { node->left = new TreeNode(std::stoi(nodes[i])); q.push(node->left); } i++; if (i < nodes.size() && nodes[i] != "null") { node->right = new TreeNode(std::stoi(nodes[i])); q.push(node->right); } i++; } return root;}// 简单的前序遍历用于验证结果void preorder(TreeNode* root) { if (!root) { std::cout << "null "; return; } std::cout << root->val << " "; preorder(root->left); preorder(root->right);}int main() { std::string data = "1,2,3,null,null,4,5"; TreeNode* root = deserialize(data); std::cout << "反序列化后的前序遍历: "; preorder(root); std::cout << std::endl; return 0;}unique_ptr)避免内存泄漏。通过本文,你已经掌握了如何在 C++ 中实现 二叉树反序列化。这项技能不仅对理解 C++数据结构教程 至关重要,也是解决算法面试题的关键工具。希望你能动手实践,加深理解!
如果你正在准备面试,不妨多练习 LeetCode二叉树反序列化 相关题目,巩固所学知识。
本文由主机测评网于2025-12-08发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025124825.html