Lintcode_7 Binary Tree Serialization

本文介绍了一种使用BFS实现二叉树序列化与反序列化的算法。该算法将二叉树结构转化为字符串形式进行存储,并能够从字符串还原成原始的二叉树结构。文章提供了详细的代码实现。

Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.

There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.

Example

An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:

  3
 / \
9  20
  /  \
 15   7

Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.

You can use other method to do serializaiton and deserialization.

基本思路是用BFS解决,由tree生成string的时候没什么难度,注意由string生成tree的时候,参数类型标为引用,或者指针,这样等到

每一层都遍历完的时候,就剩下下一层了~~~

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * This method will be invoked first, you should design your own algorithm 
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    string serialize(TreeNode *root) {
        // write your code here
        string res;
        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty()) {
            TreeNode* node = que.front();
            que.pop();
            string input;
            if (node != NULL) {
                stringstream ss;
                ss << node->val;
                ss >> input;
                que.push(node->left);
                que.push(node->right);
            } else {
                input = "#";
            }
            input+=",";
            res+=input;
        }
        res = res.substr(0, res.size() - 1);
        return res;
    }

    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in 
     * "serialize" method.
     */
    TreeNode *deserialize(string data) {
        // write your code here
        TreeNode* root = NULL;
        vector<int> pause = {-1};
        vector<string> data_1;
        for (int i = 0; i < data.size(); i++) {
            if (data[i] == ',') {
                pause.push_back(i);
            }
        }
        pause.push_back(data.size());
        for (int i = 0; i < pause.size()-1; i++) {
            data_1.push_back(data.substr(pause[i] + 1, pause[i + 1] - pause[i]- 1));
            // cout<<data_1[i]<<endl;
        }
        queue<pair<TreeNode*, vector<string>* >> que;
        if (data_1[0] == "#")
            return root;
        stringstream ss;
        ss << data_1[0];
        int a;
        ss >> a;
        root = new TreeNode(a);
        data_1.erase(data_1.begin());
        que.push(make_pair(root, &data_1));
        // que.push(make_pair(root,&data_1));
        while (!que.empty()) {
            TreeNode* node = que.front().first;
            vector<string>& data_temp = *que.front().second;
            if (data_temp[0] != "#") {
                int a;
                stringstream ss;
                ss << data_temp[0];
                ss >> a;
                node->left = new TreeNode(a);
                que.push(make_pair(node->left, &data_temp));
            }
            data_temp.erase(data_temp.begin());
            if (data_temp[0] != "#") {
                int a;
                stringstream ss;
                ss << data_temp[0];
                ss >> a;
                node->right = new TreeNode(a);
                que.push(make_pair(node->right, &data_temp));
            }
            data_temp.erase(data_temp.begin());
            que.pop();
        }
        return root;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值