Runtime error: member access within misaligned address, which requires 8 byte alignment


  • 0
    L

    Could anyone tell me where is wrong with my method? I've been stuck here for the whole day. Thanks a lot.

    class Codec {
    public:
    
        // Encodes a tree to a single string.
        string serialize(TreeNode* root) {
            if(!root) return "";
            string res = "";
            queue<TreeNode*> q;
            q.push(root);
            bool end = true;
            while(true) {
                int size = q.size();
                for(int i = 0; i < size; ++i) {
                    TreeNode* tmp = q.front(); q.pop();
                    if(!tmp) {
                        res = res + "NULL,";
                        q.push(NULL);
                        q.push(NULL);
                    }
                    else {
                        res = res + to_string(tmp->val) + ',';
                        if(tmp->left || tmp->right) end = false;
                        q.push(tmp->left);
                        q.push(tmp->right);
                    }
                }
                if(end) break;
                end = true;
            }
            return res;
        }
    
        // Decodes your encoded data to tree.
        TreeNode* deserialize(string data) {
            if(data == "") return NULL;
            queue<string> q;
            stringstream ss;
            ss.str(data);
            string tmp;
            while(getline(ss, tmp, ',')) {
                q.push(tmp);
            }
            TreeNode* root = new TreeNode(stoi(q.front())); q.pop();
            queue<TreeNode*> q2;
            q2.push(root);
            int c = 1;
            while(!q.empty()) {
                for(int i = 0; i < c; ++i) {
                    string tl = q.front(); q.pop();
                    string tr = q.front(); q.pop();
                    TreeNode* t2 = q2.front(); q2.pop();
                    if(t2 == NULL) continue;
                    if(tl != "NULL") {
                        t2->left = new TreeNode(stoi(tl));
                        q2.push(t2->left);
                    }
                    else q2.push(NULL);
                    if(tr != "NULL") {
                        t2->right = new TreeNode(stoi(tr));
                        q2.push(t2->right);
                    }
                    else q2.push(NULL);
                }
                c *= 2;
            }
            return root;
        }
    };
    

Log in to reply
 

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