38. 序列化和反序列化二叉树

2019-08-07  本文已影响0人  HamletSunS

题目:
直白些翻译就是把一个二叉树转换成一个字符串(序列化),同时你又可以根据这个字符串把这棵二叉树还原回来(反序列化)

思路:

难点:
序列化过程:

反序列化过程:

指针型的引用变量:

代码:
100%正确的代码

class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string str;
        if(root==nullptr)
            return str;//不要返回NULL或nullptr,否则反序列化会出错,
//因为cpp中无法判断string对象是否为null值
        
        serialize(root,str);
        return str;
    }
    
    void serialize(TreeNode *root,string &str){
        if(root==nullptr){
            str+='#';
            return ;
        }
        str+=to_string(root->val);
        str+=',';
        serialize(root->left,str);
        serialize(root->right,str);
        
         
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        if(data.empty())
            return nullptr;
        int n=0;
        return deserialize(data,n);
    }
    //使用&str可以节省大量内存空间,也会提高程序运行效率
    TreeNode *deserialize(string &str,int &index){
        if(str.empty())
            return nullptr;
        if(str[index]=='#')
            {
            index++;
            return nullptr;}
        int num=0;
        bool flag=false;
        if(str[index]=='-'){
            flag=true;
            index++;
        }
        while(str[index]!=',' && index<str.size()){
            num*=10;
            num+=str[index]-'0';
            index++;
        }
        if(flag)
            num*=-1;
        TreeNode *node=new TreeNode(num);
        if(index==str.size())
            return node;
        else
            index++;
        node->left=deserialize(str,index);
        node->right=deserialize(str,index);
        return node;
    }
};

牛客网上可以通过,但实际上可能会有些问题

class Solution {
public:
    char* Serialize(TreeNode *root) {    
       string str;  
       Serialize(root,str);
        int n=str.size();
       char *ret=new char[n+1];
        for(int i=0;i<n;i++)
            ret[i]=str[i];
        ret[n]='\0';
        return ret;
    }
    TreeNode* Deserialize(char *str) {
        if(str==nullptr)
            return nullptr;
        TreeNode *root=Deserialize2(str);
        return root;
    }
    TreeNode *Deserialize2(char *&str){
        
        if(str==nullptr)
            return nullptr;
        
        if(*str=='#'){
            str++;
            return nullptr;
        }
        int num=0;
        while(*str!=',' && *str!='\0')
           { num=num*10+*str-'0';
               str++;
           }
        TreeNode *node=new TreeNode(num);
       if(*str=='\0')
          return node;
        else
            str++;
        node->left=Deserialize2(str);
        node->right=Deserialize2(str);
        return node;
    }
    void Serialize(TreeNode *root,string &str){
        if(root==nullptr)
            {str+='#';
             return ;
            }
        
        str+=to_string(root->val);
        str+=',';
        Serialize(root->left,str);
        Serialize(root->right,str);
    }
};
上一篇 下一篇

猜你喜欢

热点阅读