Leetcode 133. Clone Graph-----java

Source: Internet
Author: User

Clone an undirected graph. Each node in the graph contains a and label a lists of its neighbors .


OJ ' s undirected graph serialization:

Nodes is labeled uniquely.

We use #As a separator for each node, and ,As a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2} .

The graph has a total of three nodes, and therefore contains three parts as separated by # .

    1. First node is labeled as 0 . Connect node to 0 both nodes 1 and 2 .
    2. Second node is labeled as 1 . Connect node to 1 node 2 .
    3. Third node is labeled as 2 . Connect node 2 to Node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1      /      /       0---2         /          \_/

Copy the diagram. There are two methods of Dfs and BFS, I chose the BFs method.

/*** Definition for undirected graph. * Class Undirectedgraphnode {* int label; * LIST<UNDIRECTEDGRAPHNODE&G T Neighbors; * Undirectedgraphnode (int x) {label = x; neighbors = new arraylist<undirectedgraphnode> ();}}; */ Public classSolution { Publicundirectedgraphnode Clonegraph (Undirectedgraphnode node) {if(node = =NULL )            return NULL; Map Map=NewHashmap<integer,undirectedgraphnode>(); Queue Queue=NewLinkedlist<undirectedgraphnode>();        Queue.add (node); Undirectedgraphnode nnn=NewUndirectedgraphnode (Node.label);        Map.put (NNN.LABEL,NNN);  while( !Queue.isempty ()) {Undirectedgraphnode nn=(Undirectedgraphnode) queue.poll (); List ll1=nn.neighbors; Undirectedgraphnode nn2=(Undirectedgraphnode) map.get (Nn.label); List Ll2=nn2.neighbors;  for(inti = 0;i<ll1.size (); i++) {Undirectedgraphnode Node2=(Undirectedgraphnode) ll1.get (i); if(Map.containskey (Node2.label)) {Ll2.add (Map.get (Node2.label)); }Else{Undirectedgraphnode Node3=NewUndirectedgraphnode (Node2.label);                    Map.put (NODE2.LABEL,NODE3);                    Ll2.add (NODE3);                Queue.add (Node2); }            }        }            returnnnn;}}

Leetcode 133. Clone Graph-----java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.