zoukankan      html  css  js  c++  java
  • 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));
  • 相关阅读:
    春季学期第十二周作业
    2019春第三次课程设计实验报告
    春季学期第十一周作业
    春季学期第十周作业
    春季学期第九周作业
    第五周总结 & 实验报告(三)
    第四周总结 & 实验报告(二)
    实验报告(一)&第三周总结
    第二周小结
    2019春总结
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5417934.html
Copyright © 2011-2022 走看看