zoukankan      html  css  js  c++  java
  • 297. 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.

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

    Credits:
    Special thanks to @Louis1992 for adding this problem and creating all test cases.

    Subscribe to see which companies asked this question

     

     思路:

        采用前序遍历的方法。在序列化的时候,遇到空节点,返回的字符串加上NULL,遇到非空节点,返回的字符串加上节点的值。递归得到最终的序列字符串。保证在节点之间由逗号隔开。

        在反序列化的时候,指明从start位置开始寻找逗号的位置,这样就可以截取节点对应的字符串。遇到“NULL”,直接返回NULL。否则根据字符串的值建立根节点,递归建立左右子树。

    /**
     * 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 result ="";
            preorder(root,result);
            return result;
        }
        void preorder(TreeNode *root ,string &result){
            if(result.length()>0)
                result.push_back(',');
            if(!root){
                result+="NULL";
                return;
            }
            result+=to_string(root->val);
            preorder(root->left,result);
            preorder(root->right,result);
        }
        // Decodes your encoded data to tree.
        TreeNode* deserialize(string data) {
            int start=0;
            return de_preorder(data,start);
        }
        TreeNode *de_preorder(string&data,int &start){
            if(start>=data.length())
                return NULL;
            int index =data.find(',',start);
            if(index ==string::npos){
                index =data.length();
            }
            string tmp =data.substr(start,index -start);
            start=index+1;
            if(tmp=="NULL"){
                return NULL;
            }
            TreeNode *root =new TreeNode (stoi(tmp));
            root->left =de_preorder(data,start);
            root->right=de_preorder(data,start);
            return root;
        }
    };
    // Your Codec object will be instantiated and called as such:
    // Codec codec;
    // codec.deserialize(codec.serialize(root));
  • 相关阅读:
    分布式搜索引擎Elasticsearch的查询与过滤
    剖析Elasticsearch集群系列第一篇 Elasticsearch的存储模型和读写操作
    分布式缓存 cachecloud
    npm是什么NPM的全称是Node Package Manager
    Grafana监控可视化环境搭建
    github ssl验证跳过
    Linux分区扩容
    手把手教你把Vim改装成一个IDE编程环境(图文)
    根据条件批量删除document
    奇智网络聊天机器人
  • 原文地址:https://www.cnblogs.com/zhoudayang/p/5280218.html
Copyright © 2011-2022 走看看