Given n
nodes labeled from 0
n-1
to and a list of undirected edges (each edge is a pair of nodes), write a funct Ion to check whether these edges make up a valid tree.
The main problem is to Judge 1. Is there a loop 2. Whether it is a connected graph
You can use DFS, BFS and Union find,union find most appropriate.
For DFS and BFS, the first is to establish the adjacent list, the edges connection of all the conditions to join the adjacency list, notice the need to join symmetric, for [A, a], to a B, B to join a. You can use visited to record whether or not you have traversed.
1. BFS
BFS basically is implemented with the queue, first add the first point, each time poll out a point, on its adjacency linked list traversal, queue, note to the point from its traversal to the neighboring linked list of the point of the list of adjacent links, to ensure that a side will not go back and forth two times (why the front to join the symmetry and here to delete aside, Because you do not know the point of the connection between the situation is how, in the course of the traverse may be from the direction of A->b may also be the opposite, if only one, in the connection graph when the connected side will be broken off. Count records the number of points connected to the connected graph, and the last and N ratios.
Again, for a list such as get to the point, is an object, not an int, when assigning to int (int), when using remove, note that remove is an object, not int,remove (int index), remove ( Object OB), both of which are to be separated, here is to remove the object, so Add (Integer).
classSolution { Public BooleanValidtree (intNint[] edges) { if(n==0)return false; List[] List=NewList[n]; for(inti=0;i<n;i++) {List[i]=NewArraylist<integer>(); } for(int[] pair:edges) {list[pair[0]].add (pair[1]); list[pair[1]].add (pair[0]); } Queue<Integer> queue=NewLinkedlist<>(); Queue.offer (0); Boolean[] visited=New Boolean[n]; intCount=1; while(!Queue.isempty ()) { intA=Queue.poll (); if(Visited[a])return false; Visited[a]=true; for(intI=0;i<list[a].size (); i++){ intM= (int) List[a].get (i);//Note to go intQueue.offer (m); Count++; List[m].remove ((Integer) a);//Note to add (Integer), otherwise it is considered to be index, delete } } returncount==N; }}
2.DFS
The pre must be recorded in DFS to avoid a->b->a such traversal, which is considered a loop.
classSolution { Public BooleanValidtree (intNint[] edges) { if(n==0)return false; List[] List=NewList[n]; for(inti=0;i<n;i++) {List[i]=NewArraylist<integer>(); } for(int[] pair:edges) {list[pair[0]].add (pair[1]); list[pair[1]].add (pair[0]); } Boolean[] visited=New Boolean[n]; if(Dfs (list, visited, 0,-1))return false; for(inti=0;i<n;i++){ if(!visited[i])return false; } return true; } Public BooleanDFS (list[) List,Boolean[] visited,intIdintpre) { if(Visited[id])return true; Visited[id]=true; for(intI=0;i<list[id].size (); i++){ if(pre== (int) List[id].get (i))Continue; if(Dfs (list, visited, (int) List[id].get (i), id))return true; } return false; }}
3. Union Find
Use count to record the number of connected graphs, each of which shows one less split block, and if the last becomes 1, it is all connected.
classSolution { Public BooleanValidtree (intNint[] edges) { int[] connect=New int[n]; if(edges.length!=n-1)return false; for(inti=0;i<n;i++) {Connect[i]=i; } intCount=N; for(inti=0;i<edges.length;i++){ intA=findroot (connect,edges[i][0]); intB=findroot (connect,edges[i][1]); if(a==b)return false; Count--; CONNECT[B]=A; } returnCount==1; } Public intFindrootint[] Connect,intID) { while(Connect[id]!=id) id=Connect[id]; returnID; }}
Leetcode 261-graph Valid Tree (Medium) (BFS, DFS, Union find)