/**
 * 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.
     */
    void bTreeTraverseByBFS(TreeNode* root, vector<TreeNode*>& bTree) {
        queue<TreeNode*> q;
        q.push(root);

        while(!q.empty()) {
            TreeNode* cur = q.front();
            q.pop();
            bTree.push_back(cur);
            if (cur == NULL) {
                continue;
            } else {
                q.push(cur->left);
                q.push(cur->right);
            }
        }

        while (bTree.size() > 0 && bTree[bTree.size() - 1] == NULL) {
            bTree.pop_back();
        }

        return;
    }

    string vecToString(vector<TreeNode*>& bTree) {
        stringstream ss;
        ss << "{" << bTree[0]->val;
        for (int i = 1; i < bTree.size(); i++) {
            if (bTree[i] == NULL) {
                ss << ",#";
            } else {
                ss << "," << bTree[i]->val;
            }
        }
        ss << "}";
        return ss.str();
    }

    string serialize(TreeNode *root) {
        // write your code here
        if (root == NULL) {
            return "{}";
        }
        vector<TreeNode*> bTree;
        bTreeTraverseByBFS(root, bTree);
        return vecToString(bTree);
    }

    /**
     * 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.
     */
    vector<string> split(string s, char delimiter) {
        vector<string> results;
        string cur = "";
        int index = 0;
        while (index < s.length()) {
            if (s[index] != delimiter) {
                cur += s[index];
                index++;
            } else {
                results.push_back(cur);
                cur = "";
                index++;
            }
        }
        results.push_back(cur);
        return results;
    }

    TreeNode* restoreTree(vector<string> nodes) {
        TreeNode* root = new TreeNode(stoi(nodes[0]));
        queue<TreeNode*> q;
        q.push(root);
        bool isLeftChild = true;
        for (int i = 1; i < nodes.size(); i++) {
            if (nodes[i] != "#") {
                if (isLeftChild) {
                    q.front()->left = new TreeNode(stoi(nodes[i]));
                    q.push(q.front()->left);
                } else {
                    q.front()->right = new TreeNode(stoi(nodes[i]));
                    q.push(q.front()->right);                    
                }
            }
            if (!isLeftChild) {
                q.pop();
            }
            isLeftChild = !isLeftChild;
        }
        return root;
    }

    TreeNode *deserialize(string data) {
        // write your code here
        if (data == "{}") {
            return NULL;
        }
        vector<string> nodes = split(data.substr(1, data.length() - 2), ',');
        return restoreTree(nodes);
    }
};

results matching ""

    No results matching ""