问题描述

133. Clone Graph【LeetCode】

之前总是把引用符号掉了,错了好多次。。

代码如下

/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:
    UndirectedGraphNode *clone(UndirectedGraphNode *node,map<int,UndirectedGraphNode *>&m)
    {
        if(node==NULL) return NULL;
        if(m.find(node->label) != m.end())
            return m[node->label];
        UndirectedGraphNode*temp= new UndirectedGraphNode(node->label);
        m[node->label] = temp;
        for(int i=0;i<node->neighbors.size();i++)
        {
            UndirectedGraphNode *newptr = clone(node->neighbors[i],m);
            temp->neighbors.push_back(newptr);
        }
        return temp;
    }
    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
    	if(node==NULL) return NULL;
        map<int,UndirectedGraphNode*> m;
        return clone(node,m);
    }
};

相关文章:

  • 2021-10-23
  • 2022-01-07
  • 2021-08-10
  • 2022-12-23
  • 2021-08-19
  • 2021-06-26
猜你喜欢
  • 2021-05-23
  • 2021-09-20
  • 2021-05-17
  • 2021-11-08
  • 2022-02-07
  • 2021-07-07
  • 2021-06-02
相关资源
相似解决方案