Dfs

95 articles

dsa2 min read

Flood Fill — DFS Color Replace

Implement the flood fill algorithm (like paint bucket tool): replace all cells of the same color reachable from a starting cell.

Read →
dsa2 min read

Surrounded Regions — Boundary DFS

Capture all 'O' regions not connected to the border. Classic boundary DFS: mark safe cells from edges, then flip remaining O to X.

Read →
dsa2 min read

Rotting Oranges — Multi-Source BFS

Find minimum minutes until all oranges rot. Multi-source BFS: push all initially-rotten oranges into queue simultaneously, BFS layer by layer.

Read →
dsa2 min read

Clone Graph — BFS/DFS with HashMap

Deep copy an undirected graph. BFS/DFS with a HashMap from original node to cloned node prevents revisiting and handles cycles.

Read →
dsa1 min read

Open the Lock — BFS State Space

Minimum turns to reach target combination avoiding deadends. BFS over all 4-digit wheel states with bidirectional BFS optimization.

Read →
dsa1 min read

Course Schedule II — Topological Order

Return a valid course order given prerequisites. Kahn's BFS topological sort outputs nodes in processing order — that is the valid schedule.

Read →
dsa2 min read

Word Ladder — BFS Shortest Transformation

Find minimum transformations from beginWord to endWord changing one letter at a time (each intermediate must be in wordList). Classic BFS on word states.

Read →
dsa1 min read

Bus Routes — BFS on Route Graph

Find minimum buses to get from source to target stop. BFS where nodes are routes (buses), not stops. Jump to all stops of a route, then to all routes passing through each stop.

Read →
dsa2 min read

Sum Root to Leaf Numbers

Compute the total sum of all root-to-leaf numbers formed by concatenating digits along each path.

Read →
dsa2 min read

Convert BST to Greater Tree

Replace each node value with the sum of all values greater than or equal to it using reverse in-order traversal.

Read →
dsa2 min read

Find Duplicate Subtrees

Find all duplicate subtrees by serializing each subtree and using a hash map to detect duplicates.

Read →
dsa2 min read

Distribute Coins in Binary Tree

Count minimum moves to distribute coins evenly by tracking excess/deficit coins flowing through each edge.

Read →
dsa2 min read

Find Leaves of Binary Tree

Collect leaves by height (distance from leaf) repeatedly, returning all leaves at each height level.

Read →
dsa3 min read

Sum of Distances in Tree

Compute sum of distances from every node to all others in O(n) using two DFS passes with rerooting technique.

Read →
dsa1 min read

Binary Tree Pruning

Remove all subtrees that do not contain a 1 by recursively returning null for subtrees with only zeros.

Read →
dsa2 min read

Path Sum IV

Sum all root-to-leaf path sums in a tree encoded as 3-digit integers using a hashmap to decode tree structure.

Read →
dsa2 min read

Binary Tree Upside Down

Transform a binary tree so every right child becomes a sibling and left child becomes parent using iterative rotation.

Read →
dsa2 min read

Reconstruct Itinerary — Hierholzer's Eulerian Path

Find the itinerary using all flight tickets exactly once (Eulerian path). Uses Hierholzer's algorithm: DFS with post-order insertion into result — the only graph algorithm that uses post-order for path construction.

Read →