MLE


  • 0
    N

    Hi, my code get MLE for the last test case, could anyone plz tell me why ?

    class Codec {
    private:
        void dfs(TreeNode* root, string& str) {
            if (root) {
                str += to_string(root->val) + ',';
                dfs(root->left, str);
                dfs(root->right, str);
            }
            
            return;
        }
        
        TreeNode* helper(string str, int& pos, int min, int max) {
            if (pos == str.size())
                return NULL;
            int cur_pos = pos, value;
            while (str[pos] != ',') {
                pos++;
            }
            
            value = stoi(str.substr(cur_pos, pos - cur_pos));
            if (value < min || value > max) {
                pos = cur_pos;
                return NULL;
            }
            
            pos++;  
            TreeNode *root = new TreeNode(value);
            root->left = helper(str, pos, min, value);
            root->right = helper(str, pos, value, max);
            return root;
        }
    public:
    
        // Encodes a tree to a single string.
        string serialize(TreeNode* root) {
            string preorder;
            dfs(root, preorder);
            return preorder;
        }
    
        // Decodes your encoded data to tree.
        TreeNode* deserialize(string data) {
            int pos = 0;
            
            return helper(data, pos, INT_MIN, INT_MAX);
        }
    };
    
    

Log in to reply
 

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