LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

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   5

as "[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.

这题实际上思路还是比较清晰的,遇见NULL的话那么存入一个#既可以了,解序列化的时候直接相应的处理#就可以了,代码如下:

  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 
 13     // Encodes a tree to a single string.
 14     string serialize(TreeNode* root) {
 15         queue<TreeNode *> q;
 16         string str = "";
 17         if(!root) return str;
 18         stringstream ss;
 19         ss << root->val;
 20         str += ss.str();
 21         q.push(root);
 22         while(!q.empty()){
 23             TreeNode * t = q.front();
 24             if(t->left) q.push(t->left);
 25             if(t->right) q.push(t->right);
 26             if(t->left){
 27                 ss.str("");
 28                 ss << "," << t->left->val;
 29                 str += ss.str();
 30             }else{
 31                 ss.str("");
 32                 ss << "," << "#";
 33                 str += ss.str(); 
 34             }
 35 
 36             if(t->right){
 37                 ss.str("");
 38                 ss << "," << t->right->val;
 39                 str += ss.str();
 40             }else{
 41                 ss.str("");
 42                 ss << "," << "#";
 43                 str += ss.str();
 44             }
 45             q.pop();
 46         }
 47         //删除末尾的#字符
 48         for(int i = str.length() - 1; ; ){
 49             if(str[i] == '#'){
 50                 str = str.substr(0, i - 1);
 51                 i = str.length() - 1;
 52             }else{
 53                 break;
 54             }
 55         }
 56         return str;
 57     }
 58 
 59     // Decodes your encoded data to tree.
 60     TreeNode* deserialize(string data) {
 61         if(!data.size()) return NULL;
 62         vector<string> dataVec;
 63         int sz = data.length();
 64         for(int i = 0; i < sz;){//分割字符串,保存到vector中
 65             if(data[i] != ','){
 66                 int j = i;
 67                 for(; j < sz && data[j] != ','; ++j)
 68                     ;
 69                 string tmp = data.substr(i, j - i);//注意substr的使用细节
 70                 dataVec.push_back(tmp);
 71                 i = j + 1;  //跳过','符号
 72             }
 73         }
 74         sz = dataVec.size();
 75         TreeNode * root = new TreeNode(atoi(dataVec[0].c_str()));
 76         queue<TreeNode *>q;
 77         q.push(root);
 78         int  i = 1;
 79         while(!q.empty() && i < sz){
 80             TreeNode * t = q.front();
 81             if(dataVec[i] != "#"){
 82                 t->left = new TreeNode(atoi(dataVec[i].c_str()));
 83                 q.push(t->left);
 84             }else{
 85                 t->left = NULL;
 86             }
 87             i++;
 88             if(i >= sz) break;
 89             if(dataVec[i] != "#"){
 90                 t->right = new TreeNode(atoi(dataVec[i].c_str()));
 91                 q.push(t->right);
 92             }else{
 93                 t->right = NULL;
 94             }
 95             q.pop();
 96             i++;
 97         }
 98         return root;
 99     }
100 };

吐槽一下,c++连个split函数都没有,写起来真是恶心,还要自己去分割字符串。

posted @ 2015-10-27 10:08  eversliver  阅读(266)  评论(0编辑  收藏  举报