LeetCode: Clone Graph [133]

【题目】

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

OJ‘s undirected graph serialization:

Nodes are 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 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
         /          \_/

【题意】

复制无向图。无向图中每个点有一个值唯一的label和邻接节点的label集合

【思路】

DFS或者BFS遍历无向图完成赋值

【注意】本题中使用malloc创建节点会报RUNTIME ERROR错误,必须用new创建节点

【代码】

/**
 * Definition for undirected graph.
 * struct UndirectedGraphNode {
 *     int label;
 *     vector<UndirectedGraphNode *> neighbors;
 *     UndirectedGraphNode(int x) : label(x) {};
 * };
 */
class Solution {
public:

    UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
        if(node==NULL)return NULL;

        queue<UndirectedGraphNode*> qe;
        queue<UndirectedGraphNode*> qe_copy;
        map<int, UndirectedGraphNode*> createdMap;      //已经复制的节点

        //创建第一个节点
        //注意本题使用(UndirectedGraphNode*)malloc(sizeof(UndirectedGraphNode))会RUNTIME ERROR
        UndirectedGraphNode * newFirstNode = new UndirectedGraphNode(node->label);
        qe.push(node);
        qe_copy.push(newFirstNode);
        createdMap[node->label]=newFirstNode;

        while(!qe.empty()){
            UndirectedGraphNode * qe_top = qe.front(); qe.pop();
            UndirectedGraphNode * qe_copy_top = qe_copy.front();qe_copy.pop();
            //创建邻接节点
            int neighborsSize = qe_top->neighbors.size();
            for(int i=0; i<neighborsSize; i++){
                node = qe_top->neighbors[i];
                //判断当前节点是否已经创建过
                if(createdMap[node->label]!=NULL){
                    //如果已经创建了,直接从createdmap中取即可
                    qe_copy_top->neighbors.push_back(createdMap[node->label]);
                }
                else{
                    //如果还没创建,则创建新节点
                    //注意本题使用(UndirectedGraphNode*)malloc(sizeof(UndirectedGraphNode))会RUNTIME ERROR
                    UndirectedGraphNode * node_copy = new UndirectedGraphNode(node->label);
                    qe.push(node);
                    qe_copy.push(node_copy);
                    createdMap[node->label]=node_copy;
                    //添加到qe_copy_top的邻接节点
                    qe_copy_top->neighbors.push_back(node_copy);
                }
            }
        }
        return newFirstNode;
    }
};

LeetCode: Clone Graph [133]

时间: 2024-10-27 12:31:14

LeetCode: Clone Graph [133]的相关文章

[LeetCode]Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

[LeetCode] Clone Graph 无向图的复制

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

leetcode题目:Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

Clone Graph leetcode java(DFS and BFS 基础)

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

【leetcode】Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

leetcode -day11 Clone Graph &amp; Palindrome Partitioning I II

 1.Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

[Leetcode][JAVA] Clone Graph, Copy List with Random Pointer

Clone Graph: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label a