LintCode-7-二叉树的序列化和反序列化

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

设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。
如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构。

注意事项

There is no limit of how you deserialize or serialize a binary tree, LintCode will take your output of serialize as the input of deserialize, it won't check the result of serialize.

样例

给出一个测试数据样例, 二叉树{3,9,20,#,#,15,7},表示如下的树结构:

我们的数据是进行BFS遍历得到的。当你测试结果wrong answer时,你可以作为输入调试你的代码。
你可以采用其他的方法进行序列化和反序列化。

标签

二叉树 微软 雅虎

code

/**
 * 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:
   
    /**
     * http://www.lintcode.com/zh-cn/ladder/6/-第8章-7-二叉树的序列化和反序列化
     * 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 result = "";
        TreeNode *node=root;

        if(node == NULL)
            return result;

         preorderTraversal(node, result);

        return result;
    }

    void preorderTraversal(TreeNode *root, string &result) {

        if(root == NULL) {
            result += "#,";
        }
        else {
            char temp[20];
            sprintf(temp, "%d,", root->val);
            result += temp;
            preorderTraversal(root->left, result);
            preorderTraversal(root->right, result);
        }
    }

    /**
     * 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
        int size = data.size(),i=0;
        if(size == 0)
            return NULL;

        TreeNode *root;
        vector<string> treeArray = split(data, ",");
        depreorderTraversal(root, treeArray, i, treeArray.size());

        return root;
    }

    void depreorderTraversal(TreeNode *&root, vector<string> &treeArray, int &i, const int size) {
        if(i < size) {
            if(treeArray[i].compare("#") == 0) {
                root = NULL;
                i++;
            }
            else {
                root = (TreeNode*)malloc(sizeof(TreeNode));
                root->val = atoi(treeArray[i].c_str());
                i++;
                depreorderTraversal(root->left,treeArray, i, size);
                depreorderTraversal(root->right,treeArray, i, size);
            }
        }
    }

    vector<string> split(const string &s, const string &seperator){
        vector<string> result;
        typedef string::size_type string_size;
        string_size i = 0;

        while(i != s.size()){
            //找到字符串中首个不等于分隔符的字母;
            int flag = 0;
            while(i != s.size() && flag == 0){
                flag = 1;
                for(string_size x = 0; x < seperator.size(); ++x)
                    if(s[i] == seperator[x]){
                        ++i;
                        flag = 0;
                        break;
                }
            }

            //找到又一个分隔符,将两个分隔符之间的字符串取出;
            flag = 0;
            string_size j = i;
            while(j != s.size() && flag == 0){
                for(string_size x = 0; x < seperator.size(); ++x)
                if(s[j] == seperator[x]){
                    flag = 1;
                    break;
            }
            if(flag == 0) 
                ++j;
            }
            if(i != j){
                result.push_back(s.substr(i, j-i));
                i = j;
            }
        }
        return result;
    }
};
posted @ 2017-06-02 20:35  LiBaoquan  阅读(712)  评论(0编辑  收藏  举报