• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ArgenBarbie
博客园    首页    新随笔    联系   管理    订阅  订阅
297. Serialize and Deserialize Binary Tree *HARD*

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.

 

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string s = "";
        queue<TreeNode*> q;
        if(root)
            q.push(root);
        while(!q.empty())
        {
            TreeNode *p = q.front();
            q.pop();
            if(!p)
            {
                s += "null ";
            }
            else
            {
                s += to_string(p->val) + " ";
                q.push(p->left);
                q.push(p->right);
            }
        }
        cout<<s<<endl;
        return s;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        cout<<data<<endl;
        if(data == "")
            return NULL;
        int l = data.length(), idx1 = 0, idx2;
        queue<TreeNode*> q;
        TreeNode *root = NULL;
        bool flag = 0; //0表示该左子树,1表示该右子树
        while(idx1 < l)
        {
            idx2 = data.find(" ", idx1+1);
            string s = data.substr(idx1, idx2-idx1);
            if(s == "null")
            {
                if(flag)
                {
                    q.front()->right = NULL;
                    q.pop();
                }
                else
                    q.front()->left = NULL;
                flag = !flag;
            }
            else
            {
                int t = atoi(s.c_str());
                TreeNode *p = new TreeNode(t);
                if(!root)
                    root = p;
                else
                {
                    if(flag)
                    {
                        q.front()->right = p;
                        q.pop();
                    }
                    else
                        q.front()->left = p;
                    flag = !flag;
                }
                q.push(p);
            }
            idx1 = idx2+1;
        }
        return root;
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

 

posted on 2016-04-21 17:41  ArgenBarbie  阅读(201)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3