List<Integer> childs = map.getOrDefault(node, newArrayList<>());//to not give null pointer
for(intnext : childs){
if(!visited[next]){
queue.offer(next);
parent.put(next,node);
}elseif(parent.get(node) != next){//To ensure a node has only one parent . Check the node parent is market as child then then its ok 0-1 , 1-0 because of undirected
returnfalse;//cyclic
}
}
}
// All visited
for(booleane : visited){
if(!e) returnfalse;
}
returntrue;
}
}
classSolutionDFS {
//DFS
publicbooleanvalidTree(intn, int[][] edges) {
//in BFS if we visit all node and no cyclic in graph its a tree.