Build graph by input, use bfs to check every node's neighbor.

If the neighbor is not parent and is visited, then there is a cycle, return false.

Else put this neighbor in the queue.

```
class Solution {
public:
struct node{
int parent=0;
vector<int>neighbors;
};
bool validTree(int n, vector<pair<int, int>>& edges) {
unordered_map<int,node>nodes;
if(edges.size()!=n-1)return false;
vector<bool>v(n,false);
for(auto edge:edges){
nodes[edge.first].neighbors.push_back(edge.second);
nodes[edge.second].neighbors.push_back(edge.first);
}
queue<int>q;
q.push(0);
v[0]=true;
while(!q.empty()){
int p=q.front();
q.pop();
for(auto n:nodes[p].neighbors){
if(n!=nodes[p].parent){
if(v[n])return false;
v[n]=true;
nodes[n].parent=p;
q.push(n);
}
}
}
return true;
}
};
```