Copy List with Random Pointe
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
方法一:BFS
RandomListNode *copyRandomList(RandomListNode *head)
{
if (!head) return NULL;
unordered_map<RandomListNode*, RandomListNode*> mapping;
mapping[head] = new RandomListNode(head->label);
queue<RandomListNode*> que;
que.push(head);
while (!que.empty())
{
RandomListNode* temp = que.front();
que.pop();
if (temp->next)
{
if(mapping.find(temp->next) == mapping.end())
{
mapping[temp->next] = new RandomListNode(temp->next->label);
que.push(temp->next);
}
mapping[temp]->next = mapping[temp->next];
}
if (temp->random)
{
if(mapping.find(temp->random) == mapping.end())
{
mapping[temp->random] = new RandomListNode(temp->random->label);
que.push(temp->random);
}
mapping[temp]->random = mapping[temp->random];
}
}
return mapping[head];
}
方法二:DFS
unordered_map<RandomListNode*, RandomListNode*> mapping;
RandomListNode *copyRandomList(RandomListNode *head) {
// 如果为NULL直接返回NULL
if (!head) return NULL;
// 判断节点参数是否已经在映射对中,如果没有则新建节点,并对其指针进行赋值
// 新节点的每个指针都是对下一层的搜索,不存在则新建,存在则直接返回映射对
if (mapping.find(head) == mapping.end())
{
mapping[head] = new RandomListNode(head->label);
mapping[head]->next = copyRandomList(head->next);
mapping[head]->random = copyRandomList(head->random);
}
return mapping[head];
}
问题二:
Clone Graph
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
方法一:BFS
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node) return NULL;
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> mapping;
mapping[node] = new UndirectedGraphNode(node->label);
queue<UndirectedGraphNode*> que;
que.push(node);
while(!que.empty())
{
UndirectedGraphNode* temp = que.front();
que.pop();
for (int i = 0; i < temp->neighbors.size(); ++i)
{
if (mapping.find(temp->neighbors[i]) == mapping.end())
{
mapping[temp->neighbors[i]] = new UndirectedGraphNode(temp->neighbors[i]->label);
que.push(temp->neighbors[i]);
}
mapping[temp]->neighbors.push_back(mapping[temp->neighbors[i]]);
}
}
return mapping[node];
}
方法二:DFS
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> mapping;
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node) return NULL;
if (mapping.find(node) == mapping.end())
{
mapping[node] = new UndirectedGraphNode(node->label);
for (int i = 0; i < node->neighbors.size(); ++i)
mapping[node]->neighbors.push_back(cloneGraph(node->neighbors[i]));
}
return mapping[node];
}
总结:
1、以上两种方法都用到了相同的思路,针对结构复杂的数据进行深度复制时采用了映射关系的unordered_map,从原结构指针索引新结构指针,映射关系为
Old List --map--> New List
node map[node]
head map[head]
2、该方法适用于数据节点中有随机指针(Random Pointer)的情况。
附原UP主代码地址:https://leetcode.com/discuss/29710/bfs-and-dfs-with-hash-unordered_map-in-c