Skip to main content

3 posts tagged with "Stack"

View All Tags

Iterative DFS with stack-based graph traversal

Daniel Farlow
Software Engineer

Depth-first search (DFS) on a graph (binary tree or otherwise) is most often implemented recursively, but there are occasions where it may be desirable to consider an iterative approach instead. Such as when we may be worried about overflowing the call stack. In such cases it makes sense to rely on implementing DFS with our own stack instead of relying on our program's implicit call stack. But doing so can lead to some problems if we are not careful.