Why am i getting MLE?


  • 0
    I
    /**
     * 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:
        void preorder(TreeNode* root,string& str)
        {
            if(root==NULL)
            {
                str=str+":,";
            }
            else
            {
                str=str+to_string(root->val)+",";
                preorder(root->left,str);
                preorder(root->right,str);
            }
        }
        TreeNode* preorder_open(string data,int* m)
        {
            if((*m)>=data.length())
            return NULL;
            else
            {
                string temp="";
                while((*m)<data.length()-1 && data[*m]!=',')
                {
                    temp=temp+data[*m];
                    (*m)++;
                }
                (*m)++;
                if(temp==":")
                {
                    return NULL;
                }
                int value=stoi(temp);
                TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));
                root->val=value;
                root->left=preorder_open(data,m);
                root->right=preorder_open(data,m);
                return root;
            }
        }
       
        string serialize(TreeNode* root) {
            string str="";
            preorder(root,str);
            return str;
            
        }
    
       
        TreeNode* deserialize(string data) {
            int m=0;
            TreeNode* root=preorder_open(data,&m);
            return root;
            
        }
    };

  • 0
    J

    The MLE test case is a tree with 1000 nodes and the depth is also 1000. So using DFS approach could easily use up the system stack.


Log in to reply
 

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