## When to use dfs? Generally speaking, there are several situations as follows:

### Combination

Sample answer: *combination sum2 *

Key Point: 1) sorting before dfs 2) use Index to help remove duplicates 3) consider out of boundary question whenever using arrays

Use DFS without recursion: Use **STACK(Every time pop an item, push the it’s children, thus accomplish DFS)**

Stack FILO keeps pushing into stack and pop out the newest one. **When to use Stack DFS**: when we want to get something ITERATIVELY, we can’t do dfs every time to get the next smallest. (sample: BFS iterator)

Advertisements