#algorithm #search #graph

idea

Traverse a graph going all the way down one node first.

Recursively:

  1. Call DFS recursively
  2. Process current node.

Iteratively, use a stack, and array of visited items

  1. Add root to stack, mark visited
  2. Till stack is not empty
  3. Pop element, process.
  4. Add all adjacent nodes to stack
  5. Go to 2.

Three orders for trees

  1. Pre-order: Visit root, then left then right.
  2. In-order: Visit left, then root, then right
  3. Post-order: Visit left, right, then root.