The six traversal templates every fresher is asked to write: BFS and DFS for a binary tree, for a graph stored as an adjacency list, and for a 2D grid, each in Python and the same lines in Java, each with its visited-set rule stated. Then a page on when BFS beats DFS and when DFS is simpler, four fully worked problems with traces (level order, number of islands, shortest path in a grid with obstacles, cycle in an undirected graph), and the five mistakes that cost marks. Every Python snippet was run and matches the output printed beside it. 14 pages, PDF.
Step 3 of 5: Pass the coding test
BFS and DFS Templates: Six Templates, Java and Python
Free
No payment. Just your email.
- 14 pages
- 6 templates
- Tree, graph, grid
Free. Six templates.
Tree, graph and grid, in Java and Python, ready to type.
We ask for your email on the next step and send the file straight away.
About this guide
What you download
- BFS_And_DFS_Templates.pdf846 KB


