449. Serialize and Deserialize BST

/**
 * 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:


    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        ostringstream out;
        serialize(root,out);
        return out.str();
    }


    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        istringstream in(data);
        return deserialize(in);
    }
    
private:
    void serialize(TreeNode* root, ostringstream& out)
    {
        if(NULL == root) 
        {
            //out<<"#";
            out<<"# ";
            return;
        };
        out<<root->val<<" ";
        //out<<root->val<<' ';
        //if(root->left) serialize(root->left,out);        
        //if(root->right) serialize(root->right,out);
        serialize(root->left, out);
        serialize(root->right, out);
    };  
    TreeNode* deserialize(istringstream& in)
    {
            string val;
            in>>val;
            if(val == "#") return NULL;
            TreeNode* p = new TreeNode(stoi(val));   // stoi
            p->left = deserialize(in);
            p->right = deserialize(in);
            
            return p;
                                   
    };
    
                                  
};
*/
// Accpetion of mine

// no duplicated node.

// 序列化为前序和中序的字符串,然后重新构建树。

class Codec {
public:


// Encodes a tree to a single string.
string serialize(TreeNode* root) {
        string ans;
ostringstream outInorder;
        ostringstream outPreorder;
        inOrder(root,outInorder);
        preOrder(root,outPreorder);
return outInorder.str()+";"+outPreorder.str();
}


// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data.size() == 0) return NULL;
        int nPos = data.find(";");
        string strInorder = data.substr(0,nPos);
        string strPreorder = data.substr(nPos+1);
        
        int val;
        vector<int> v_inorder;
        vector<int> v_preorder;
        
        istringstream inIn(strInorder);
        istringstream inPre(strPreorder);
        
        while(inIn>>val)  v_inorder.push_back(val);     
        while(inPre>>val)  v_preorder.push_back(val);
        
        return createTree(0,0,v_inorder.size()-1,v_preorder,v_inorder);
}


private:
void inOrder(TreeNode* root, ostringstream& out)
{
if(root == NULL)
        {
            return;
        }        
        inOrder(root->left,out);
        out<<root->val<<" ";
        inOrder(root->right,out);
}
void preOrder(TreeNode* root, ostringstream& out)
{
        if(root == NULL)
        {
            return;
        }
        out<<root->val<<" ";
        preOrder(root->left,out);
        preOrder(root->right,out);
}
TreeNode* createTree(int startPre, int startIn, int endIn, vector<int>& vPre, vector<int>& vIn)
    {
        if(startPre >=vPre.size() || startIn > endIn ) return NULL;
        TreeNode* root = new TreeNode(vPre[startPre]);
        int inIndex = 0;
        //for(int i = 0; i<vIn.size();i++)
        for (int i = startIn; i<=endIn;i++)
        {
            if(vIn[i] == vPre[startPre]) {inIndex = i; break;};
        }
        
        root->left = createTree(startPre+1,startIn, inIndex-1,vPre,vIn);
        root->right = createTree(startPre+inIndex-startIn+1,inIndex+1, endIn,vPre,vIn);       
        
        
        return root;
    };
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

猜你喜欢

转载自blog.csdn.net/bjzhaoxiao/article/details/80264503
今日推荐