Graphs (Depth-first traversal)

The nodes are traversed from top to bottom. (Reading time: under 2 minutes)

To traverse a graph depth-first, we need to use a stack. We manually pick one node to start with, as there is no specific root node like you have with a binary search tree, and go down each of the children nodes. In this example, green nodes are marked as visited nodes, blue nodes are currently being visited.

Get hands-on with 1200+ tech skills courses.