<@U5UU34LPK> commented on <@U0ZFBBUBU>’s file <htt...
# announcements
u
@karelpeeters commented on @groostav’s file https://kotlinlang.slack.com/files/U0ZFBBUBU/F93G02DS8/-.kt: Unlesss I'm missing something
Copy code
fun Node.bfs(): Sequence<Node> {
    val start = generateSequence(this) { it.parents.first() }.last()
    val queue: Queue<Node> = LinkedList<Node>().also { it.add(start) }
    val visited = HashSet<Node>()

    return buildSequence {
        while(queue.isNotEmpty()){
			val next = queue.remove()
			if (next in visited)
				continue

            yield(next)

            visited += next
            queue += next.children.filter { it !in queue }
        }
    }
}
should also conform to your specifications.