DFS notes

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

Combination

Sample answer: combination sum2 

bfs-combination.png

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s