序列化和反序列化二叉树
序列化其实就是二叉树遍历,根据特定的要求按照要求格式输出就行
反序列化跟序列化对应上,一般来讲只知道某一个遍历的序列是无法还原二叉树的,但是题目给出的序列带有nullptr空指针,所以是可以的
中序遍历是无法反序列化的,因为不知道根节点的具体索引
前序、后序、层序都可以进行反序列化,以下给出了前序遍历序列化反序列化和层序遍历序列化反序列化的代码
前序遍历的顺序进行序列化和反序列化
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #include<cstring> // stoi函数头文件,是C语言中的函数 class Codec { private: string NUL = "null"; string SEP = ","; // 前序遍历 void traverse(TreeNode* root, string& str) { if (root == nullptr) { // 字符串拼接 str += NUL; str += SEP; return; } str += to_string(root->val); // to_string函数定义在头文件string中,将数字转换成字符串 str += SEP; traverse(root->left, str); traverse(root->right, str); } TreeNode* deserializeNode(queue<string>& que) { if (que.empty()) return nullptr; string first = que.front(); que.pop(); if (first == "null") return nullptr; TreeNode* root = new TreeNode(stoi(first)); root->left = deserializeNode(que); root->right = deserializeNode(que); return root; } public: // Encodes a tree to a single string. string serialize(TreeNode* root) { string res; // 保存序列化的结果 traverse(root, res); return res; } // Decodes your encoded data to tree. TreeNode* deserialize(string data) { if (data.empty()) return nullptr; // 去掉逗号,其他字符串放到队列里,方便递归函数使用 queue<string> que; int begin = 0; int end = 0; while (begin < data.size()) { while (end < data.size() && data[end] != ',') end++; string str = data.substr(begin, end - begin); // 分割子串,从begin位置开始的end-begin个元素 que.push(str); // 将逗号分隔的字符串放到队列里 begin = ++end; } return deserializeNode(que); } }; // Your Codec object will be instantiated and called as such: // Codec codec; // codec.deserialize(codec.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #include<cstring> // stoi函数头文件,是C语言中的函数 class Codec { private: string NUL = "null"; string SEP = ","; // 层序遍历进行序列化 void traverse(TreeNode* root, string& str) { if (root == nullptr) return ""; queue<TreeNode*> que; que.push(root); while (!que.empty()) { int size = que.size(); for (int i = 0; i < size; i++) { TreeNode* node = que.front(); que.pop(); if (node == nullptr) { str += NUL; str += SEP; } str += to_string(node->val); str += NUL; str += SEP; que.push(node->left); que.push(node->right); } } } TreeNode* deserializeNode(vector<string>& nodes) { if (nodes.empty()) return nullptr; TreeNode* root = new TreeNode(atoi(nodes[0])); // 先构造根节点 queue<TreeNode*> que; // 用来记录父节点 que.push(root); for (int i = 1; i < nodes.size(); i++) { TreeNode* node = que.top(); que.pop(); string left = nodes[i++]; if (left != NUL) { node->left = new TreeNode(atoi(left)); que.push(node->left); } else { node->left = nullptr; } string right = nodes[i++]; if (right != NUL) { node->right = new TreeNode(atoi(right)); que.push(node->right); } else { node->right = nullptr; } } return root; } public: // Encodes a tree to a single string. string serialize(TreeNode* root) { string res; // 保存序列化的结果 traverse(root, res); return res; } // Decodes your encoded data to tree. TreeNode* deserialize(string data) { if (data.empty()) return nullptr; // 去掉逗号,其他字符串放到数组里,方便递归函数使用 vector<string> nodes; int begin = 0; int end = 0; while (begin < data.size()) { while (end < data.size() && data[end] != ',') end++; string str = data.substr(begin, end - begin); // 分割子串,从begin位置开始的end-begin个元素 nodes.push_back(str); // 将逗号分隔的字符串放到队列里 begin = ++end; } return deserializeNode(nodes); } }; // Your Codec object will be instantiated and called as such: // Codec codec; // codec.deserialize(codec.serialize(root));