[Leetcode] Clone Graph

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 have a total of three nodes, and therefore contains three parts as Separated By # .

  1. first node is labeled As 0 . Connect node 0  to both Nodes 1  and 2 .
  2. second node is labeled As 1 . Connect node 1  to 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         /          \_/

Hide TagsDepth-first Search Breadth-first Search Graph


topic requires copying a diagramAccording to test instructions we find that lable is unique and it marks a node. So we can use the associative container map and use lable as key to save the addresses of the points that have been created.a recursive version is written first:
Class Solution {Public:<span style= "White-space:pre" ></span>undirectedgraphnode *cloneGraph ( Undirectedgraphnode *node) {<span style= "White-space:pre" ></span>if (NULL = = node) return Null;<span Style= "White-space:pre" ></span>map<int, Undirectedgraphnode *> occured;<span style= "White-space: Pre "></span>return Clone (node, occured); <span style=" White-space:pre "></span>}<span style= "White-space:pre" ></span>undirectedgraphnode *clone (Undirectedgraphnode *node, Map<int, Undirectedgraphnode *> &occured) {<span style= "White-space:pre" ></span>if (Occured.count (node- >label) = = 0) {<span style= "white-space:pre" ></span>undirectedgraphnode *newnode = new Undirectedgraphnode (Node->label); <span style= "White-space:pre" ></span>occured[node->label] = Newnode;<span style= "White-space:pre" ></span>for (int i = 0; i < node->neighbors.size (); i++) {< Span style= "White-space:pre "></span>newnode->neighbors.push_back (Clone (node->neighbors[i), occured));< Span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>return NewNode; <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>else{<span Style= "White-space:pre" ></span>return occured[node->label];<span style= "White-space:pre" ></ Span>}<span style= "White-space:pre" ></span>}};

and then wrote a non-recursive version, this time with each node in the original address as key, to store the address of the corresponding new nodeuse a queue to save the node you want to access
Class Solution {Public:undirectedgraphnode *clonegraph (Undirectedgraphnode *node) {if (NULL = = node) return null;map< Undirectedgraphnode *, Undirectedgraphnode *> occured;queue<undirectedgraphnode *> visit;visit.push (node); while (!visit.empty ()) {Undirectedgraphnode *cur = Visit.front (); Visit.pop (); if (occured.count (cur) = = 0) { Undirectedgraphnode *newnode = new Undirectedgraphnode (Cur->label); occured[cur] = NewNode;} for (int i = 0; i < cur->neighbors.size (); i++) {Undirectedgraphnode *tmp = cur->neighbors[i];if (Occured.count (tm p) = = 0) {Undirectedgraphnode *newnode = new Undirectedgraphnode (Tmp->label); occured[tmp] = Newnode;visit.push (TMP);} Occured[cur]->neighbors.push_back (Occured[tmp]);}} return occured[node];};

the two-segment code is all over the AC

[Leetcode] Clone Graph

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.