我的算法7

题目地址https://leetcode.com/problems/serialize-and-deserialize-binary-tree/#/description

题目描述: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.

我的代码

/**
 * 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:
    void add_tree_to_str(TreeNode* root,string& str){
        if(root==NULL){
            str+="null,";
            return;
        }
        str+=(to_string(root->val)+",");
        add_tree_to_str(root->left,str);
        add_tree_to_str(root->right,str);
    }
    void add_str_to_tree(TreeNode* tree,string &data,int& start){
        int now=start;
        for(;start<data.size();start++)
            if(data[start]==',')break;
        if(data.substr(now,start-now)!="null"){
            tree->left=new TreeNode(atoi(data.substr(now,start-now).c_str()));
            start++;
            add_str_to_tree(tree->left,data,start);
        }
        start++;
        now=start;
        for(;start<data.size();start++)
            if(data[start]==',')break;
        if(data.substr(now,start-now)!="null"){
            tree->right=new TreeNode(atoi(data.substr(now,start-now).c_str()));
            start++;
            add_str_to_tree(tree->right,data,start);
        }

    }

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string str="";
        if(!root) return str;
        add_tree_to_str(root,str);
        return str;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        TreeNode* tree;
        if(data=="") return tree;
        int start=0;
        for(;start<data.size();start++)
            if(data[start]==',')
                break;
        string s=data.substr(0,start);
        tree=new TreeNode(atoi(s.c_str()));
        start++;
        add_str_to_tree(tree,data,start);
        return tree;
    }
};

解题思路
我的答案采用前序遍历来转换树与字符串,为方便区分左子树与右子树,我将所有的空节点(包括叶节点的两个空子节点)表示为null存储在字符串中,于是通过递归调用很容易将树转换成字符串。前序遍历的复杂度为O(n)。
然后是将前序遍历后生产的字符串转换为树。仍然采用递归调用。对任意非空树,先保存其根节点,然后对根节点递归获取其左子树,然后再获取其又子树。由于所有的空节点,包括叶节点的两个空节点都用“null”表示在字符串中。所以递归到其叶节点后,递归过程会自然的结束。由于该过程对字符串只需顺序遍历一次,所以复杂度为O(n)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值