[Python] I created BFS using recursion. I passed some test cases. I wanna know if my algo is...

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I marked all the current nodes children in visitors, then called the children's bfs function to mimic bfs. I wanna know if the algo logic is correct. Code:

    class Solution:
    # Function to return Breadth First Search Traversal of given graph.
    def bfs(self, adj):
    # code here
    visitors = [0]*len(adj)
    lst = []
    for i in range(len(adj)):
    if visitors == 0:
    lst.append(i)
    visitors = 1
    self.bfs_func(i, adj, visitors, lst)
    return lst
    def bfs_func(self, i, adj, visitors, lst):
    base = []
    for x in adj:
    if visitors[x] == 0:
    visitors[x] = 1
    lst.append(x)
    base.append(x)
    for y in base:
    self.bfs_func(y, adj, visitors, lst)

    Continue reading...

Compartilhe esta Página