Why MLE for the 2000 node test case?


  • 0
    M

    Hello! I shared the same idea with the DFS solutions in the discussion. But my code got MLE for the 2000 node test case. Can anyone tell me why? Here is my code:

        int flag=0, ct=0;
        bool validTree(int n, vector<pair<int, int>>& edges) {
            vector<vector<int>> graph(n);
            vector<bool> v(n, false);
            for(int i=0;i<edges.size();i++)
            {
                graph[edges[i].first].push_back(edges[i].second);
                graph[edges[i].second].push_back(edges[i].first);
            }
            DFS(0, v, graph, -1);
            return flag==0&&ct==n;
        }
        
        void DFS(int r, vector<bool>& visited, vector<vector<int>> graph, int pre) {
            if(flag==1) return;
            visited[r]=true;
            ct++;
            for(int i=0;i<graph[r].size();i++)
            {
                if(visited[graph[r][i]]&&graph[r][i]!=pre) 
                {
                    flag=1;
                    return;
                }
                if(graph[r][i]==pre) continue;
                //pre[graph[r][i]]=r;
                DFS(graph[r][i], visited, graph, r);
            }
        }

Log in to reply
 

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