讨论/《深度优先搜索》 - 练习:克隆图/
《深度优先搜索》 - 练习:克隆图
共 1 个回复
class Solution {
public:
     Node* cloneGraph(Node* node) {
        if (node==NULL) {
            return NULL;
        }
        if (!copies.count(node) ) {
            copies[node] = new Node(node -> val);
            for (Node* neighbor : node -> neighbors) {
                copies[node] -> neighbors.push_back(cloneGraph(neighbor));
            }   
        }

        return copies[node];
    }
private:
    map<Node*, Node*> copies;
};