天天看点

[CareerCup] 13.7 Node Pointer 节点指针

13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed in data structure. The Node data structure contains two pointers to other Nodes.

在这道题让我们通过一个节点指针来复制整个数据结构,节点类Node中包含两个节点指针,我们需要用哈希表来建立原数据结构中每一个节点的地址到相对应的新结构中的地址,这样我们就可以在用DFS的时候知道哪些节点我们已经拷贝过了,就可以直接跳过。通过这种方式来标记访问过的节点可以不用在节点内部存储。拷贝过程如下所示: