Tags

DFS

DFS

1 lesson
41 problems
2 code snippets
1 question bank
9 community items

dfs

Algorithms

1 lesson

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

Practice Problems

41 problems

Balanced Binary Tree

Free
Not Started
Easy

Given the root of a binary tree, determine if it is height-balanced (the depth of the two subtrees of every node never differs by more than one).

Binary Tree
DFS
Recursion
Beginner

517

13

Count Complete Tree Nodes

Free
Not Started
Easy

Given the root of a complete binary tree, return the number of nodes in the tree.

Binary Tree
DFS
Binary Search
Recursion
Beginner

499

7

Diameter of Binary Tree

Free
Not Started
Easy

Given the root of a binary tree, return the length of the diameter (the longest path between any two nodes, measured in edges).

Binary Tree
DFS
Recursion
Beginner

1.1k

29

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

Lowest Common Ancestor of a BST

Free
Not Started
Easy

Given a binary search tree and two nodes, find their lowest common ancestor by leveraging the BST property.

Binary Tree
Binary Search Tree (BST)
DFS
Recursion
Lowest Common Ancestor (LCA)
Beginner

902

17

Maximum Depth of Binary Tree

Free
Not Started
Easy

Given the root of a binary tree, return its maximum depth (the number of nodes along the longest path from root to leaf).

Binary Tree
DFS
Recursion
Beginner

181

6

Path Sum

Free
Not Started
Easy

Given the root of a binary tree and a target sum, determine if the tree has a root-to-leaf path that sums to the target.

Binary Tree
DFS
Recursion
Path Problems
Beginner

863

9

Same Tree

Free
Not Started
Easy

Given the roots of two binary trees, determine if they are the same (structurally identical with the same node values).

Binary Tree
DFS
Recursion
Beginner

508

12

Subtree of Another Tree

Free
Not Started
Easy

Given the roots of two binary trees, determine if the second tree is a subtree of the first tree.

Binary Tree
DFS
Recursion
Subtree Problems
Beginner

284

9

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

Binary Tree Maximum Path Sum

Free
Not Started
Hard

Given the root of a binary tree, return the maximum path sum where a path is any sequence of connected nodes (not necessarily passing through the root).

Binary Tree
DFS
Recursion
Dynamic Programming
Advanced

580

6

Longest Increasing Path in a Matrix

Not Started
Hard

Given an m x n integer matrix, find the length of the longest strictly increasing path where you can move in four directions.

Dynamic Programming
Memoization
DFS
Matrix Traversal
Advanced

337

9

Reconstruct Itinerary

Not Started
Hard

Given a list of airline tickets, reconstruct the itinerary in lexical order starting from 'JFK', using all tickets exactly once.

Graphs
DFS
Euler Path / Circuit
Sorting
Advanced

406

2

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

Word Search II

Not Started
Hard

Given a 2D board of characters and a list of words, find all words that can be formed by sequentially adjacent cells on the board, using each cell at most once per word.

Trie / Prefix Tree
Trie Operations
Backtracking
DFS
Arrays
Strings
Advanced

1.1k

38

Accounts Merge

Not Started
Medium

Given a list of accounts where each account has a name and emails, merge accounts that share a common email address.

Graphs
Union-Find / DSU
DFS
Hash Map / Dictionary
Intermediate

155

5

Design Add and Search Words

Not Started
Medium

Design a data structure that supports adding words and searching for words with wildcard characters, where '.' can match any single letter.

Trie / Prefix Tree
Trie Operations
DFS
Design Patterns
Strings
Intermediate

1k

14

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

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

Number of Connected Components

Not Started
Medium

Given n nodes and a list of undirected edges, find the number of connected components in the graph.

Graphs
DFS
Union-Find / DSU
Connected Components
Undirected Graphs
Intermediate

1k

24

Construct BT from Inorder and Postorder

Free
Not Started
Medium

Given inorder and postorder traversal arrays, construct the binary tree and return its root.

Binary Tree
DFS
Recursion
Tree Traversal
Intermediate

663

2

Construct BT from Preorder and Inorder

Free
Not Started
Medium

Given preorder and inorder traversal arrays, construct the binary tree and return its root.

Binary Tree
DFS
Recursion
Tree Traversal
Intermediate

803

23

Count Good Nodes in Binary Tree

Free
Not Started
Medium

Given a binary tree root, count the number of 'good' nodes where no node on the path from root to that node has a value greater than the node's value.

Binary Tree
DFS
Recursion
Intermediate

333

10

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

Flatten Binary Tree to Linked List

Free
Not Started
Medium

Given the root of a binary tree, flatten the tree into a linked list in-place using the right pointers in preorder traversal order.

Binary Tree
DFS
Recursion
Intermediate

261

2

Graph Valid Tree

Not Started
Medium

Given n nodes and a list of undirected edges, determine if these edges form a valid tree (connected and acyclic).

Graphs
DFS
Union-Find / DSU
Cycle Detection
Undirected Graphs
Intermediate

932

16

Kth Smallest Element in a BST

Free
Not Started
Medium

Given the root of a BST and an integer k, return the kth smallest value (1-indexed) using in-order traversal.

Binary Tree
Binary Search Tree (BST)
DFS
Inorder
Stack
Intermediate

613

18

Lowest Common Ancestor of Binary Tree

Free
Not Started
Medium

Given a binary tree and two nodes, find their lowest common ancestor (the deepest node that is an ancestor of both).

Binary Tree
DFS
Recursion
Lowest Common Ancestor (LCA)
Intermediate

501

2

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 Absolute Difference in BST

Free
Not Started
Medium

Given a BST, find the minimum absolute difference between the values of any two different nodes using in-order traversal.

Binary Tree
Binary Search Tree (BST)
DFS
Inorder
Intermediate

702

20

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

Number of Provinces

Free
Not Started
Medium

Given an adjacency matrix representing connections between cities, find the total number of provinces (connected components).

Graphs
DFS
Union-Find / DSU
Connected Components
Undirected Graphs
Intermediate

1.1k

18

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

Path Sum II

Free
Not Started
Medium

Given the root of a binary tree and a target sum, return all root-to-leaf paths where the sum of the values equals the target.

Binary Tree
DFS
Backtracking
Path Problems
Intermediate

1.1k

27

Sum Root to Leaf Numbers

Free
Not Started
Medium

Given a binary tree where each node contains a digit 0-9, find the total sum of all root-to-leaf numbers.

Binary Tree
DFS
Recursion
Path Problems
Intermediate

367

8

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

Validate Binary Search Tree

Free
Not Started
Medium

Given the root of a binary tree, determine if it is a valid binary search tree using bounds checking or in-order traversal.

Binary Tree
Binary Search Tree (BST)
DFS
Recursion
Inorder
Intermediate

739

18

Word Search

Free
Not Started
Medium

Given an m x n grid of characters and a string word, determine if the word exists in the grid by following a path of adjacent cells (horizontally or vertically) without reusing any cell.

Arrays
Backtracking
Recursion
DFS
Algorithms
Intermediate

710

2

Community

9 items
Problem
Medium
Free

House Robber III

Maximize money robbed from a binary-tree neighborhood where no two directly-connected houses can both be robbed.

trees
binary-tree
dfs
recursion

869

7

4.3 (12)

May 16, 2026

by @milozhang

Article

Graph Algorithms 101

Why most production graph work is modeling, not algorithm; the two traversals you actually use; and the representation table that drives every memory tradeoff.

graph-algorithms
bfs
dfs
graph-representation
algorithms

739

8

4.4 (9)

Apr 4, 2026

by @antonmorgan

Article

Backtracking: The Template That Solves 30 Problems

One choose, explore, unchoose template. Walk it on N-queens, then map the variations: permutations, subsets, word-search, sudoku. The pattern stays put; the predicate moves.

backtracking
recursion
algorithms
graph-traversal-patterns
dfs

241

4

4.2 (15)

Mar 24, 2026

by @lucasmoreau

Problem
Medium
$6.99

Recover Binary Search Tree

Two BST nodes have been swapped by mistake. Restore the BST in-place by finding and swapping them back.

trees
bst
dfs
recursion

921

5

4.3 (15)

Mar 22, 2026

by @ananyanakamura

Problem
Easy
Free

Cousins in Binary Tree

Decide whether two values in a binary tree are cousins (same depth, different parents).

trees
binary-tree
bfs
dfs

655

20

4.4 (12)

Mar 14, 2026

by @arjunpatel

Problem
Easy
Free

Two Sum IV - Input is a BST

Decide whether two distinct BST nodes sum to a target, ideally in O(n) time and O(h) extra space.

trees
bst
two-pointers
dfs

418

9

Feb 28, 2026

by @owentoure

Problem
Medium
Free

Is Graph Bipartite?

Decide whether an undirected graph admits a 2-coloring with no edge sharing a color, using BFS over each component.

graphs
bfs
dfs
graph-coloring

975

3

4.5 (17)

Feb 26, 2026

by CodeSnatch

Problem
Easy
Free

Range Sum of BST

Sum every BST value within an inclusive range, pruning subtrees that fall outside.

trees
bst
dfs
recursion

506

16

Feb 16, 2026

by @ninarossi

Problem
Easy
Free

Binary Tree Paths

Return every root-to-leaf path in a binary tree as a list of arrow-joined strings.

trees
binary-tree
dfs
recursion

813

14

4.5 (10)

Jan 29, 2026

by CodeSnatch