
转自stackoverflow
DFS:
1 2 3 4 5 6
|
list nodes_to_visit = {root}; while( nodes_to_visit isn't empty ) { currentnode = nodes_to_visit.take_first(); nodes_to_visit.prepend( currentnode.children ); }
|
BFS:
1 2 3 4 5 6
|
list nodes_to_visit = {root}; while( nodes_to_visit isn't empty ) { currentnode = nodes_to_visit.take_first(); nodes_to_visit.append( currentnode.children ); }
|
注意唯一的区别就是DFS是prepend,添加在最前。而BFS是append,添加在最后。
近期评论