• 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));
  • 相关阅读:
    IOS开发防止图片渲染的方法
    IOS界面通信-代理(协议)传值
    IOS打开其他应用、以及被其他应用打开
    IOS UITableView的分隔线多出问题
    self.view 的不当操作造成死循环
    IOS 导航栏属性设置
    在iOS 8及以后使用UIAlertController 等各种弹出警告通知
    iOS通过URL构建UIImage
    自定义 URL Scheme 完全指南
    Unknown type name 'NSString' 解决方案
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5417934.html
Copyright © 2020-2023  润新知