@jianchao-li-fighter

Hello! my solution shares the same idea but my code got MLE for the 2000 node test case. Could you tell me why? Thanks!

class Solution {
public:
int flag=0, ct=0;
bool validTree(int n, vector<pair<int, int>>& edges) {
vector<vector<int>> graph(n);
vector<int> pre(n, -1), v(n, 0);
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<int>& visited, vector<vector<int>> graph, int pre) {
if(flag==1) return;
visited[r]=1;
ct++;
for(int i=0;i<graph[r].size();i++)
{
if(visited[graph[r][i]]==1&&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);
}
}
};