C++ same as leetcode's


  • 0
    X
    /**
     * 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:
        //level order
        // Encodes a tree to a single string.
        string serialize(TreeNode* root) {
            if(root == NULL){
                return "[]";
            }
            string ans = "[";
            bool first = true;
            TreeNode* flag = new TreeNode(-1);
            queue<TreeNode*> q;
            q.push(root);
            while(!q.empty()){
                TreeNode* tmp = q.front();
                q.pop();
                if(tmp == flag){
                    if(q.empty()){
                        break;
                    }else{
                        q.push(flag);
                    }
                }else{
                    if(first == true){
                        first = false;
                    }else{
                        ans += ",";
                    }
                    if(tmp == NULL){
                        ans += "null";
                    }else{
                        ans += int2string(tmp->val);
                        q.push(tmp->left);
                        q.push(tmp->right);
                    }
                }
            }
            return ans + "]";
        }
    
        // Decodes your encoded data to treeq.
        TreeNode* deserialize(string data) {
            int length = data.length();
            if(length <= 2){
                return NULL;
            }
            data = data.substr(1,  length - 2);
            TreeNode* root = new TreeNode(-1);
            queue<TreeNode**> q;
            q.push(&root);
            int num, sign;
            for(int i = 0; i < length; ++i){
                TreeNode** tmp = q.front();
                q.pop();
                if(data[i] == 'n'){
                    delete *tmp;
                    *tmp = NULL;
                    while(i < length && data[i] != ','){
                        ++i;
                    }
                }else{
                    num = 0;
                    sign = 1;
                    if(data[i] == '-'){
                        sign = -1;
                        ++i;
                    }
                    while(i < length && data[i] != ','){
                        num = num * 10 + data[i] - '0';
                        ++i;
                    }
                    num *= sign;
                    (*tmp)->val = num;
                    (*tmp)->left = new TreeNode(-1);
                    (*tmp)->right = new TreeNode(-1);
                    q.push(&((*tmp)->left));
                    q.push(&((*tmp)->right));
                }
            }
            return root;
        }
    private:
        string int2string(int x){
            stringstream ss;
            ss << x;
            string tmp;
            ss >> tmp;
            return tmp;
        }
        int string2int(string s){
            stringstream ss;
            ss << s;
            int x;
            ss >> x;
            return x;
        }
    };
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec;
    // codec.deserialize(codec.serialize(root));

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.