Tags

BFS

BFS

3 lessons
27 problems
3 code snippets
2 question banks
6 community items

bfs

Data Structures

1 lesson

Queue & Deque

Free
Beginner

45 min

1 prereq

BFS visits a graph one frontier at a time, and the trick that makes that work is a queue: nodes are added in the order they are discovered and pulled out in the same order, level by level. **Queue & Deque** captures that First In, First Out discipline as a reusable abstraction, then extends it to a double-ended variant that pops up surprisingly often in sliding-window problems. In this lesson, you will implement `enqueue`, `dequeue`, `front`, and `isEmpty` using both a linked list and an array, then see why a naive array queue wastes space and how a circular queue with two index pointers fixes that with `O(1)` operations. You will also build a deque that supports inserts and removes at both ends, the structure underneath the monotonic-deque pattern that solves sliding-window-maximum in linear time. In **Stack (LIFO)**, you implemented `push` and `pop` from the same end and saw how the choice of array versus linked list affected the worst-case bound. A queue uses the same two storage options but pulls from the opposite end, which is what forces the circular buffer trick when you back it with an array. With a queue in your toolkit, **Hash Map (Dictionary) Basics** introduces the most-used data structure in real-world code: a key-value store that answers lookups in expected `O(1)`.

Not Started

0%

Queue
FIFO
Deque
Circular Queue
Data Structures
Beginner
Free
BFS

Algorithms

2 lessons

BFS & DFS (Intro)

Free
Beginner

60 min

3 prereqs

Swap a queue for a stack inside a graph traversal and you flip from breadth-first to depth-first behavior. The visited-vertex tracking is identical, the neighbor expansion is identical, and yet one explores level by level while the other dives down the longest path first. That single data-structure swap is the entire difference between the two most-used graph algorithms in the field. **BFS & DFS (Intro)** walks through both. For BFS you will implement queue-based level-by-level exploration and use it to find shortest paths in unweighted graphs. For DFS you will see both the recursive form (which uses the call stack implicitly) and the iterative form with an explicit stack, and apply them to connectivity checks, path finding, and cycle detection. The lesson also covers the visited set that prevents infinite loops on cyclic graphs and contrasts graph traversal with tree traversal as a special case. This lesson assumes you are comfortable with three structures from the data-structures track: **Stack (LIFO)** powers iterative DFS, **Queue & Deque** powers BFS, and **Graphs: Representation Basics** gives you the adjacency list or matrix that both algorithms read from. Next, **Hashing Techniques** turns to a different building block, the hash map, which underpins many of the lookup-heavy patterns you will combine with traversal in later lessons.

Not Started

0%

Algorithms
BFS
DFS
Graphs
Graph Traversal Patterns
Visited Set
Beginner
Free

Advanced Graph Algorithms (Network Flow)

Advanced

80 min

1 prereq

Two seemingly different problems, the maximum amount of water you can push from a source to a sink through a network of capacitated pipes, and the minimum total capacity you must remove to disconnect the sink from the source, turn out to have the same answer for every graph. The max-flow min-cut theorem ties them together, and once you accept it a surprising number of optimization problems (image segmentation, project selection, bipartite matching) collapse into max-flow instances. **Advanced Graph Algorithms (Network Flow)** introduces the algorithms that compute that maximum flow. You will work through the Ford-Fulkerson method built on augmenting paths and the residual graph, the BFS-based Edmonds-Karp variant with its `O(V * E^2)` guarantee, and Dinic's algorithm which uses level graphs and blocking flows to reach `O(V^2 * E)`. The lesson then turns to maximum bipartite matching (Hungarian and Hopcroft-Karp), applications like airline scheduling and image segmentation, and a first look at minimum-cost flow. In **Graph Algorithms (Advanced)**, you handled MST, all-pairs shortest paths, and SCCs on edge-weighted graphs. Network flow keeps the weighted-edge model but flips the question from "shortest" to "highest throughput." Next, **String Algorithms** turns to pattern matching, the implicit automaton of a regular expression.

Not Started

0%

Algorithms
Graphs
Network Flow
Ford-Fulkerson Algorithm
Bipartite Matching
Shortest Path
BFS
Advanced
Premium

Practice Problems

27 problems

Flood Fill

Free
Not Started
Easy

Given a 2D grid representing an image, a starting pixel, and a new color, perform a flood fill (like a paint bucket tool) to recolor the connected region.

Graphs
DFS
BFS
Islands / Flood Fill
Beginner

1k

9

Invert Binary Tree

Free
Not Started
Easy

Given the root of a binary tree, invert the tree (mirror it) and return its root.

Binary Tree
DFS
BFS
Recursion
Beginner

465

14

Symmetric Tree

Free
Not Started
Easy

Given the root of a binary tree, check whether it is a mirror of itself (symmetric around its center).

Binary Tree
DFS
BFS
Recursion
Beginner

457

14

Alien Dictionary

Not Started
Hard

Given a sorted list of words in an alien language, determine the order of characters in the alien alphabet using topological sort.

Graphs
Topological Sort
Directed Graphs
BFS
DFS
Advanced

910

9

Serialize and Deserialize Binary Tree

Free
Not Started
Hard

Design an algorithm to serialize a binary tree to a string and deserialize the string back to the original tree.

Binary Tree
DFS
BFS
Serialize / Deserialize
Advanced

544

8

Swim in Rising Water

Not Started
Hard

Given an n x n grid of elevations, find the minimum time t at which you can swim from the top-left to the bottom-right corner.

Graphs
BFS
Binary Search
Heap
Shortest Path
Advanced

691

15

Word Ladder

Not Started
Hard

Given a begin word, end word, and a dictionary, find the length of the shortest transformation sequence where each step changes exactly one letter.

Graphs
BFS
Shortest Path
Word Ladder
Strings
Advanced

1.1k

19

Average of Levels in Binary Tree

Free
Not Started
Medium

Given the root of a binary tree, return the average value of the nodes on each level as an array.

Binary Tree
BFS
Queue
Level Order
Intermediate

1k

23

Binary Tree Level Order Traversal

Free
Not Started
Medium

Given the root of a binary tree, return the level order traversal of its nodes' values (from left to right, level by level).

Binary Tree
BFS
Queue
Level Order
Intermediate

1.1k

19

Binary Tree Right Side View

Free
Not Started
Medium

Given the root of a binary tree, return the values of the nodes you can see when looking at the tree from the right side, ordered from top to bottom.

Binary Tree
BFS
DFS
Level Order
Intermediate

409

2

Binary Tree Zigzag Level Order Traversal

Free
Not Started
Medium

Given the root of a binary tree, return the zigzag level order traversal of its nodes' values (alternating left-to-right and right-to-left for each level).

Binary Tree
BFS
Queue
Level Order
Intermediate

745

19

Cheapest Flights Within K Stops

Not Started
Medium

Find the cheapest price to fly from a source to a destination with at most k intermediate stops, given a list of flights with prices.

Graphs
Bellman-Ford Algorithm
BFS
Shortest Path
Weighted Graphs
Directed Graphs
Dynamic Programming
Intermediate

250

3

Clone Graph

Free
Not Started
Medium

Given a reference to a node in a connected undirected graph, return a deep copy (clone) of the entire graph.

Graphs
DFS
BFS
Hash Map / Dictionary
Clone Graph
Intermediate

1k

19

Course Schedule II

Not Started
Medium

Given courses and prerequisites, return a valid order to take all courses (topological ordering), or an empty array if impossible.

Graphs
Topological Sort
BFS
Directed Graphs
Intermediate

635

11

Course Schedule

Free
Not Started
Medium

Given a number of courses and their prerequisites, determine if it is possible to finish all courses (detect if the dependency graph has a cycle).

Graphs
Topological Sort
DFS
BFS
Directed Graphs
Cycle Detection
Intermediate

268

2

Evaluate Division

Not Started
Medium

Given equations like a/b = k, answer queries asking for the result of x/y using graph traversal on a weighted directed graph.

Graphs
DFS
BFS
Weighted Graphs
Hash Map / Dictionary
Intermediate

537

12

Jump Game II

Free
Not Started
Medium

Given an integer array where each element represents the maximum jump length at that position, find the minimum number of jumps needed to reach the last index.

Greedy
BFS
Arrays
Algorithms
Intermediate

948

26

Max Area of Island

Not Started
Medium

Given a binary grid, find the maximum area of an island (connected group of 1s connected 4-directionally).

Graphs
DFS
BFS
Islands / Flood Fill
Intermediate

485

15

Minimum Height Trees

Not Started
Medium

Given a tree of n nodes, find all roots that would minimize the tree's height when the tree is rooted at them.

Graphs
BFS
Topological Sort
Undirected Graphs
Intermediate

903

8

Number of Islands

Free
Not Started
Medium

Given a 2D grid of '1's (land) and '0's (water), count the number of distinct islands formed by connected land cells.

Graphs
DFS
BFS
Islands / Flood Fill
Intermediate

392

7

Pacific Atlantic Water Flow

Not Started
Medium

Given a matrix of heights, find all cells from which water can flow to both the Pacific and Atlantic oceans.

Graphs
DFS
BFS
Multi-Source BFS
Intermediate

366

10

Populating Next Right Pointers II

Free
Not Started
Medium

Given a binary tree, populate each node's next pointer to point to its next right node. If there is no next right node, set it to null.

Binary Tree
BFS
Queue
Level Order
Intermediate

1k

30

Rotting Oranges

Free
Not Started
Medium

Given a grid where cells contain fresh oranges, rotten oranges, or are empty, determine the minimum time for all fresh oranges to rot via adjacency spreading.

Graphs
BFS
Multi-Source BFS
Islands / Flood Fill
Intermediate

292

4

Snakes and Ladders

Not Started
Medium

Given a Snakes and Ladders board, find the minimum number of dice rolls to reach the final square from square 1.

Graphs
BFS
Shortest Path
Simulation
Intermediate

920

27

Surrounded Regions

Not Started
Medium

Given a board of 'X' and 'O', capture all regions of 'O' that are completely surrounded by 'X' (not touching the border).

Graphs
DFS
BFS
Islands / Flood Fill
Intermediate

847

15

Walls and Gates

Not Started
Medium

Given a grid with walls, gates, and empty rooms, fill each empty room with the distance to its nearest gate using multi-source BFS.

Graphs
BFS
Multi-Source BFS
Intermediate

677

4

01 Matrix

Not Started
Medium

Given a binary matrix, find the distance of each cell to the nearest 0 using multi-source BFS.

Arrays
Matrix Algorithms
Matrix Traversal
BFS
Multi-Source BFS
Intermediate

602

4

Code Snippets

3 snippets
Code Snippet

BFS Traversal Template

Breadth-first search visits every reachable node in non-decreasing distance from the start, which makes it the right algorithm for shortest-path-in-unweighted-graph, level-order tree traversal, and grid-flood problems. This snippet covers the canonical queue + visited skeleton, the level-by-level form for tree-style problems, and the multi-source variant for problems like 'rotting oranges' where many start points share a frontier.

JavaScript
algorithms
bfs
code-template
queue

1.1k

29

Medium
Code Snippet
Premium

Topological Sort (Kahn's Algorithm)

Topological sort orders the nodes of a DAG so that every edge points from earlier to later. It is the algorithm behind build-system dependency resolution, course-prerequisite scheduling, and pipelined computation. This snippet covers Kahn's BFS-based algorithm with indegree tracking, a DFS-based variant that produces the same order via post-order, and a cycle-detection variant that returns null when the graph is not acyclic.

JavaScript
algorithms
topological-sort
code-template
bfs

681

7

Hard
Code Snippet

Python BFS Template

Breadth-first search visits nodes in order of distance from the start, which makes it the right tool for shortest-path-by-edge-count, level-order traversal, and 'fewest steps' search problems. Python's `collections.deque` gives you O(1) `popleft`, which is the difference between BFS and an O(n^2) accident. This entry covers the standard template, distance tracking, and a level-by-level variant.

Python
bfs
graphs
algorithms
code-template

343

8

Medium