297. Serialize and Deserialize Binary Tree

Posted ZHOU YANG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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));

 

以上是关于297. Serialize and Deserialize Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree

297.Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree