二叉树的序列化和反序列化

/**
 * 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) 
    {
        queue<TreeNode*> que;
        string strRes;
        vector<string> vec;
        char szSub[16];
        
        if(!root)
            return "null";
        
        que.push(root);
        while( !que.empty() )
        {
            if(que.front())
            {
                memset(szSub, 16, 0);
                sprintf(szSub, "%d", que.front()->val);
                vec.push_back(szSub);
            }
            else
                vec.push_back("null");
            
            if(que.front())
            {
                que.push(que.front()->left);
                que.push(que.front()->right);
            }
            que.pop();
        }
        
        int nTail = 0;
        for(nTail=vec.size()-1; nTail>=0; nTail--)
            if(vec[nTail].compare("null") != 0)
                break;
                
        for(int i=0; i<=nTail-1; i++)
        {
            strRes += vec[i];
            strRes += ",";
        }
        strRes += vec[nTail];
        
        return strRes;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) 
    {
        if(data.empty() || data.compare("null")==0)
            return NULL;
            
        queue<TreeNode*> que;
        int i;
            
        vector<string> vec = str2vec(data);
            
        TreeNode* root = new TreeNode( atoi(vec[0].c_str()) );
        que.push(root);
        
        i = 1;
        while(i<vec.size())
        {
            TreeNode* node = que.front();
            
            if(vec[i].compare("null") != 0)
            {
                node->left = new TreeNode( atoi(vec[i].c_str()) );
                que.push(node->left);
            }
            i++;
            
            if(i<vec.size() && vec[i].compare("null") != 0)
            {
                node->right = new TreeNode( atoi(vec[i].c_str()) );
                que.push(node->right);
            }
            i++;
            
            que.pop();
        }
        
        return root;
    }
    
private:
    vector<string> str2vec(string str)
    {
        vector<string> vec;
        if(str.empty())
            return vec;
        
        int i, j;
        for(i=0, j=0; j<str.size(); j++)
        {
            if(str[j]==',')
            {
                vec.push_back(str.substr(i, j-i));
                i = j+1;
            }
        }
        vec.push_back(str.substr(i, j-i));
        
        return vec;
    }
};

// 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、付费专栏及课程。

余额充值