获取节点并复制此节点以及所有相邻节点的函数,以创建与此节点完全相似的新结构。
一个
/ \
B C-E
\ /
d
应该使用新节点创建类似的网络
Node对象由
定义节点{
Arraylist邻居; //返回数组列表中的所有相邻节点
}
此代码是否有效?
public Node copyGraph(Node A){
HashTable<Node, Node> hash = new HashTable<Node, Node> ();
return copyGraphWithHash(A, hash);
}
public Node copyGraphWithHash(Node A, HashTable<Node, Node> hash){
if (A.neighbours.size() == 0)
return null;
Node newfirst = null;
if(!hash.hasKey(A))
newfirst = new Node();
hash.add(A,newfirst);
}
for ( Node n : A.neighbours()){
if (copyGraphWithHash(n, hash))
newfirst.neightbours.add(copyGraphWithHash(n, hash));
}
return newfirst;
}
请告诉我在这里缺少什么?
答案 0 :(得分:1)
此代码将以抛出堆栈溢出异常结束。
问题:
解决方案:
其他潜在问题: