4.2---找图的两个节点是否有路径

//这道题AC了,但是并不确定是否完全正确。此外要注意因为是有向图,所以既要检查a到b,还要检查b到a
public
class Path { public boolean checkPath(UndirectedGraphNode a, UndirectedGraphNode b) { return checkPath2(a,b) || checkPath2(b,a); } public boolean checkPath2(UndirectedGraphNode a, UndirectedGraphNode b) { boolean res = false; if(a.label == b.label) return true; if(a.neighbors == null) return false; for(UndirectedGraphNode temp : a.neighbors){ if(temp.label == -1){ return false; } else{ temp.label = -1; } if(checkPath(temp,b)){ return true; } } return res; } }

 

posted @ 2015-12-22 16:06  创业-李春跃-增长黑客  阅读(216)  评论(0编辑  收藏  举报