码迷,mamicode.com
首页 > 其他好文 > 详细

clone-graph

时间:2018-04-11 00:03:01      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:pre   for   code   rap   递归   directed   back   contains   one   

1. clone-graph

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

思路:dfs,其实就是递归。

 1 /**
 2  * Definition for undirected graph.
 3  * struct UndirectedGraphNode {
 4  *     int label;
 5  *     vector<UndirectedGraphNode *> neighbors;
 6  *     UndirectedGraphNode(int x) : label(x) {};
 7  * };
 8  */
 9 class Solution {
10 public:
11     unordered_map<UndirectedGraphNode *,UndirectedGraphNode *> hash;
12     UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
13         if(!node)return node;
14         if(hash.find(node)==hash.end()){
15             hash[node] = new UndirectedGraphNode(node->label);
16             for(auto x : node->neighbors){
17                 (hash[node]->neighbors).push_back(cloneGraph(x));
18             }
19         }
20         return hash[node];
21     }
22 };

 

clone-graph

标签:pre   for   code   rap   递归   directed   back   contains   one   

原文地址:https://www.cnblogs.com/xctcherry/p/8783482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!