leetcode297

题目:

序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

示例: 

你可以将以下二叉树:

    1
   / \
  2   3
     / \
    4   5

序列化为 "[1,2,3,null,null,4,5]"

题解:

奥秘--BFS

代码:

/**
 * 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 ans = "[";
        queue<TreeNode* >q;
        q.push(root);
        
        while(!q.empty())
        {
            int flag = 0;
            queue<TreeNode*>q1;
            while(!q.empty())
            {
                TreeNode* t = q.front();
                q.pop();
                if(!t) ans += "null,";
                else
                {
                    ans += to_string(t->val) + ",";
                    q1.push(t->left);
                    q1.push(t->right);
                    if(t->left || t->right) flag = 1;
                }
            }
            
            if(flag)
            {
                while(!q1.empty())
                {
                    TreeNode* t = q1.front();
                    q1.pop();
                    q.push(t);
                }
            }
        }
        ans = ans.substr(0,ans.size()-1);
        ans = ans + "]";
        return ans;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        for(int i = 0;i<data.size();i++)
        {
            if(data[i] == '[' || data[i] == ']' || data[i] == ',')
            {
                data[i] = ' ';
            }
        }
        
        //处理字符串
        stringstream ss(data);
        string s;
        vector<string>v;
        while(ss >> s)
        {
            v.push_back(s);
        }
        
        TreeNode* root = NULL;
        if(v[0] == "null") return root;
        else 
        {
            root = new TreeNode(atoi(v[0].c_str()));
        }
        
        queue<TreeNode*>q;
        q.push(root);
        int i = 1;
        while(!q.empty() && i < v.size())
        {
            int len = q.size();
            for(int j = 0;j<len;j++)
            {
                TreeNode* t = q.front();
                q.pop();
                if(v[i] != "null")
                {
                    t->left = new TreeNode(atoi(v[i].c_str()));
                }    
                i++;
                if(v[i] != "null")
                {
                    t->right = new TreeNode(atoi(v[i].c_str()));
                } 
                i++;
                if(t->left)
                    q.push(t->left);
                if(t->right)
                    q.push(t->right);
            }
                
            
        }
        return root;
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值