C++ DFS istringstream solution


  • 0
    J
    /**
     * 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) {
            if(!root) return " #";
            return to_string(root->val) +" "+serialize(root->left)+" "+serialize(root->right);
        }
    
        // Decodes your encoded data to tree.
        TreeNode* deserialize(string data) {
            
         istringstream is(data);
         return DFS(is);
        
        }
    private:
        TreeNode*DFS(istringstream& is)
        {
             string token;
             is>>token;
        
            if(token=="#") return NULL;
           TreeNode*root = new TreeNode(stoll(token));
            root->left = DFS(is);
            root->right = DFS(is);
            return root;
        }
    };
    

Log in to reply
 

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