这道题是LeetCode中为数不多的关于图的题目,不过这道题还是比较基础,就是考察图非常经典的方法:深度优先搜索和广度优先搜索。这道题用两种方法都可以解决,因为只是一个图的复制,用哪种遍历方式都可以。具体细节就不多说了,因为两种方法太常见了。这里恰好可以用旧结点和新结点的HashMap来做visited的记录。下面是广度优先搜索的代码:
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node==null) return null; LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); UndirectedGraphNode copy = new UndirectedGraphNode(node.label); map.put(node,copy); queue.offer(node); while(!queue.isEmpty()) { UndirectedGraphNode cur = queue.poll(); for(int i=0;i<cur.neighbors.size();i++) { if(!map.containsKey(cur.neighbors.get(i))) { copy = new UndirectedGraphNode(cur.neighbors.get(i).label); map.put(cur.neighbors.get(i),copy); queue.offer(cur.neighbors.get(i)); } map.get(cur).neighbors.add(map.get(cur.neighbors.get(i))); } } return map.get(node); }深度优先搜索的代码如下:
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node == null) return null; LinkedList<UndirectedGraphNode> stack = new LinkedList<UndirectedGraphNode>(); HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); stack.push(node); UndirectedGraphNode copy = new UndirectedGraphNode(node.label); map.put(node,copy); while(!stack.isEmpty()) { UndirectedGraphNode cur = stack.pop(); for(int i=0;i<cur.neighbors.size();i++) { if(!map.containsKey(cur.neighbors.get(i))) { copy = new UndirectedGraphNode(cur.neighbors.get(i).label); map.put(cur.neighbors.get(i),copy); stack.push(cur.neighbors.get(i)); } map.get(cur).neighbors.add(map.get(cur.neighbors.get(i))); } } return map.get(node); }当然深度优先搜索也可以用递归来实现,代码如下:
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node == null) return null; HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); UndirectedGraphNode copy = new UndirectedGraphNode(node.label); map.put(node,copy); helper(node,map); return copy; } private void helper(UndirectedGraphNode node, HashMap<UndirectedGraphNode, UndirectedGraphNode> map) { for(int i=0;i<node.neighbors.size();i++) { UndirectedGraphNode cur = node.neighbors.get(i); if(!map.containsKey(cur)) { UndirectedGraphNode copy = new UndirectedGraphNode(cur.label); map.put(cur,copy); helper(cur,map); } map.get(node).neighbors.add(map.get(cur)); } }这几种方法的时间复杂度都是O(n)(每个结点访问一次),而空间复杂度则是栈或者队列的大小加上HashMap的大小,也不会超过O(n)。图的两种遍历方式是比较经典的问题了,虽然在面试中出现的不多,但是还是有可能出现的,而且如果出现了就必须做好,所以大家还是得好好掌握哈。
Hi, 你好。非常感谢你的分享。有个问题向你请教:BSF DFS我都会,只是这道题我没读懂题目中怎样根据{0,1,2#1,2#2,2}画出来的树。再比如{-1,1#1}, {0,0,0} 和{0,0,0#0,0,0#0,0,0} 分别是什么样的树呢。我没读明白“We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.”是个什么意思。 谢谢。
回复删除这里就是第一个字符表示结点需要,然后逗号之后就是边,比如{0,1,2#1,2#2,2}就是0这个结点和12连边,1和2连边,2和自己连边。然后因为是无向图,边只往前看就可以哈~
删除{0,1,2#1,2#2,2}中#用来把输入分三部分么?第一部分0,1,2 Node0分别与Node1和Node2相连接;第二部分1,2 Node1和Node2连接;第三部分2,2 Node2和Node2自己连接,然后#就是用来把三部分分开对么。{0,0,0}是什么呢,三个独立Node0 互相连接么?
删除0
/ \
0 --- 0 ? 谢谢。
#是用来分开节点的,然后第一个数字是结点的label,接下来是他的边,如果0,0,0应该是0这一个结点,然后自己连自己两次,而不是三个结点哈~
删除再次感谢你耐心的解答 ^_^
删除