首页 > 代码库 > 297. Serialize and Deserialize Binary Tree
297. Serialize and Deserialize Binary Tree
297. Serialize and Deserialize Binary Tree
- Total Accepted: 47713
- Total Submissions: 149430
- Difficulty: Hard
- Contributors: Admin
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
For example, you may serialize the following tree
1 / 2 3 / 4 5as
"[1,2,3,null,null,4,5]"
, just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
Solution 1: Preorder tranversal (dfs)
对于序列化,我们从根节点开始,如果节点存在,则将值存入输出字符串流,然后分别对其左右子节点递归调用序列化函数即可。对于去序列化,我们先读入第一个字符,以此生成一个根节点,然后再对根节点的左右子节点递归调用去序列化函数即可
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Codec { 11 public: 12 //istringstream,由 istream 派生而来,提供读 string 的功能。 13 // ostringstream,由 ostream 派生而来,提供写 string 的功能。 14 // stringstream,由 iostream 派生而来,提供读写 string 的功能。 15 16 // Encodes a tree to a single string. 17 string serialize(TreeNode* root) { 18 ostringstream out; 19 dfs(root, out); 20 return out.str(); // return stream to string 21 } 22 23 // Decodes your encoded data to tree. 24 TreeNode* deserialize(string data) { 25 //instringstream in; 26 istringstream in(data); 27 //dfs_de(data, in); 28 return deserialize(in); 29 } 30 31 32 private: 33 void dfs(TreeNode* node, ostringstream& out){ 34 //if(node == NULL) return; //cannot return; need output # 35 /* 36 if(node -> left){ 37 dfs(node -> left, ) 38 }*/ 39 if(node){ 40 out << node -> val << ‘ ‘; 41 dfs(node -> left, out); 42 dfs(node -> right, out); 43 }else{ 44 out << "# ";//string "" , not char ‘‘ 45 } 46 } 47 48 TreeNode* deserialize(istringstream &in) { 49 string val; 50 in >> val; 51 if (val == "#") return nullptr; 52 TreeNode *root = new TreeNode(stoi(val));//string to int 53 root->left = deserialize(in); 54 root->right = deserialize(in); 55 return root; 56 } 57 }; 58 59 // Your Codec object will be instantiated and called as such: 60 // Codec codec; 61 // codec.deserialize(codec.serialize(root));
Solution 2: Level Sequence tranversal (non-recursion, bfs)
1 ///////////////////BFS 2 string serialize(TreeNode* root) { 3 ostringstream out; 4 queue<TreeNode *> q; 5 if(root) q.push(root);//if很重要 corner case 6 while(!q.empty()){ 7 TreeNode *tmp = q.front(); 8 q.pop(); 9 if(tmp){ 10 out << tmp -> val << ‘ ‘; 11 q.push(tmp -> left); 12 q.push(tmp -> right); 13 }else{ 14 out << "# "; 15 } 16 } 17 return out.str(); 18 } 19 20 // Decodes your encoded data to tree. 21 TreeNode* deserialize(string data) { 22 if(data.empty()) return nullptr;//string .empty() 23 istringstream in(data); 24 queue<TreeNode *> q; 25 string val; 26 in >> val; 27 TreeNode* root = new TreeNode(stoi(val)); 28 TreeNode* curr = root; 29 //q.push(root); 30 q.push(curr); 31 while(!q.empty()){ 32 TreeNode* tmp = q.front(); 33 q.pop(); 34 if(!(in >> val)) break; 35 if(val != "#"){ 36 curr = new TreeNode(stoi(val)); 37 q.push(curr); 38 tmp -> left = curr; 39 } 40 if(!(in >> val)) break; 41 if(val != "#"){ 42 curr = new TreeNode(stoi(val)); 43 q.push(curr); 44 tmp -> right = curr; 45 } 46 } 47 return root; 48 }
297. Serialize and Deserialize Binary Tree