Performing Breadth First Search recursively

后端 未结 21 2592
不思量自难忘°
不思量自难忘° 2020-11-28 01:12

Let\'s say you wanted to implement a breadth-first search of a binary tree recursively. How would you go about it?

Is it possible using only the call-stack

21条回答
  •  情话喂你
    2020-11-28 01:48

    Let v be the starting vertex

    Let G be the graph in question

    The following is the pseudo code without using queue

    Initially label v as visited as you start from v
    BFS(G,v)
        for all adjacent vertices w of v in G:
            if vertex w is not visited:
                label w as visited
        for all adjacent vertices w of v in G:
            recursively call BFS(G,w)
    

提交回复
热议问题