Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.
There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.
An example of testdata: Binary tree {3,9,20,#,#,15,7}
, denote the following structure:
3
/
9 20
/
15 7
Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.
You can use other method to do serializaiton and deserialization.
http://www.lintcode.com/en/problem/binary-tree-serialization/
序列化二叉树,随便找一种遍历实现就好了,这里我用了前序遍历,实现起来最简单,用了stringstream来保存序列化结果。在写的时候遇到一个小问题,发现自己对stringstream的实现原理完全不懂,想当然的就以为从stringstream输出后的数据就会从stringstream里删掉,其实并不是这样的,输入输出流这一块的知识日后还得再补充补充。
1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val) { 8 * this->val = val; 9 * this->left = this->right = NULL; 10 * } 11 * } 12 */ 13 class Solution { 14 private: 15 void _serialize(TreeNode *root, ostringstream &sout) { 16 if (root == NULL) { 17 sout << "# "; 18 return; 19 } 20 sout << root->val << " "; 21 _serialize(root->left, sout); 22 _serialize(root->right, sout); 23 } 24 25 TreeNode* _deserialize(istringstream &sin) { 26 char str[20]; sin >> str; 27 if (str[0] == '#') return NULL; 28 29 TreeNode *root = new TreeNode(atoi(str)); 30 root->left = _deserialize(sin); 31 root->right = _deserialize(sin); 32 return root; 33 } 34 35 public: 36 /** 37 * This method will be invoked first, you should design your own algorithm 38 * to serialize a binary tree which denote by a root node to a string which 39 * can be easily deserialized by your own "deserialize" method later. 40 */ 41 string serialize(TreeNode *root) { 42 // write your code here 43 ostringstream sout; 44 _serialize(root, sout); 45 return sout.str(); 46 } 47 48 /** 49 * This method will be invoked second, the argument data is what exactly 50 * you serialized at method "serialize", that means the data is not given by 51 * system, it's given by your own serialize method. So the format of data is 52 * designed by yourself, and deserialize it here as you serialize it in 53 * "serialize" method. 54 */ 55 TreeNode *deserialize(string data) { 56 // write your code here 57 istringstream sin(data); 58 return _deserialize(sin); 59 } 60 };
另一个地方也有一道同样的题,不过给的输入是C风格的,就用C风格的字符串又实现了一遍。
http://www.nowcoder.com/books/coding-interviews/cf7e25aa97c04cc1a68c8f040e71fb84
1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 }; 10 */ 11 class Solution { 12 private: 13 void _Serialize(TreeNode *root, char *&s) { 14 if (root == NULL) { 15 sprintf(s, "# "); 16 s += strlen(s); 17 return; 18 } 19 sprintf(s, "%d ", root->val); 20 s += strlen(s); 21 _Serialize(root->left, s); 22 _Serialize(root->right, s); 23 } 24 25 void _Deserialize(TreeNode *&root, char *&s) { 26 if (*s == '#') { 27 s += 2; 28 return; 29 } 30 char sval[20]; 31 sscanf(s, "%s ", sval); 32 s += strlen(sval) + 1; 33 root = new TreeNode(atoi(sval)); 34 _Deserialize(root->left, s); 35 _Deserialize(root->right, s); 36 } 37 38 public: 39 char* Serialize(TreeNode *root) { 40 char *ser = new char[1000000]; 41 char *s = ser; 42 _Serialize(root, s); 43 return ser; 44 } 45 46 TreeNode* Deserialize(char *str) { 47 TreeNode *root = NULL; 48 _Deserialize(root, str); 49 return root; 50 } 51 };