BFS
Tree levels and shortest path on unweighted graphs.
6 pages
- Pattern 08: BFS (Breadth-First Search)Pattern guide
One skeleton, three surfaces: tree, grid, generic graph. The line that makes it work is int size = queue.size(); — freeze it, or your levels bleed into each…
- LC 102Binary Tree Level Order Traversal (LC 102)Medium
Use a Queue to process all nodes at each depth before moving deeper. Problem walkthrough from the BFS pattern set — Java templates, complexity analysis and…
- LC 107Binary Tree Level Order Traversal II (LC 107)Medium
Same level-order BFS as LC 102, then present levels from leaf to root (bottom-up)
- LC 199Binary Tree Right Side View (LC 199)Medium
At each depth, the rightmost node (in left-to-right order) is what you see from the right side
- LC 637Average of Levels in Binary Tree (LC 637)Easy
For each level, sum all values at that level and divide by the count — use long (or double) for the sum to avoid integer overflow
- LC 111Minimum Depth of Binary Tree (LC 111)Easy
Minimum depth = shortest path from root to a leaf (node with no children). BFS finds the nearest leaf first; naive DFS can explore deep branches before a…