String concatenation through std::to_string()


  • 0
    R

    The string concatenation through "s+std::to_string(someintdata)+"."; is not working.
    I have tested the logic using output.

    FULL CODE:

    /**

    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • TreeNode *left;
      
    • TreeNode *right;
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      
    • };
      */

    void preorder(TreeNode* root,string s)
    {
    if(root==NULL) return;
    s+=std::to_string(root->val)+".";
    preorder(root->left,s);
    preorder(root->right,s);
    }

    void inorder(TreeNode* root,string s)
    {
    if(root==NULL) return;

     inorder(root->left,s);
     cout<<to_string(root->val)<<" "; s=s+std::to_string(root->val)+".";
     inorder(root->right,s);
    

    }

    class Codec {
    public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string v1="",v2="";
        preorder(root,v1);
        inorder(root,v2);
        v1=v1+"$"+v2;
        cout<<"string is"<<v1<<" ";
        return v1;
    }
    
    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        vector<int> v1,v2;
        int i=0,a;
        while(data[i]!='$')
        {   a=0;
            if(data[i]!='.')
            {   a=data[i];
                while(data[++i]!='.')
                 a=10*a+data[i];
            }
            v1.push_back(a);
        }
        
        for(int i=0;i<v1.size();i++)
         cout<<" "<<v1[i];
        return NULL;
        
    }
    

    };

    // 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.