Recall BFS to for the basic idea.

Differences compared to BFS for graphs

  • no visited array is needed, as you can ensure that every node visited is an non-visited node.
    • this is because within a tree, two distinct nodes will only have one path between them.

Visualisation

Implementation