Dfs

DFS

Forty questions focused on depth-first search, for exploring trees and graphs branch by branch.

Find if Path Exists in Graph

easy
There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).

Flood Fill

easy
You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image.

Island Perimeter

easy
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.

Accounts Merge

medium
Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails...

All Paths From Source to Target

medium
Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.

Array Nesting

medium
You are given an integer array nums of length n where nums is a permutation of the numbers in the range [0, n - 1].

Battleships in a Board

medium
Given an m x n matrix board where each cell is a battleship 'X' or empty '.', return the number of the battleships on board.

Bulb Switcher II

medium
There is a room with n bulbs labeled from 1 to n that all are turned on initially, and four buttons on the wall.

Cheapest Flights Within K Stops

medium
There are n cities connected by some number of flights.

Check if There is a Valid Path in a Grid

medium
You are given an m x n grid.

Check Knight Tour Configuration

medium
There is a knight on an n x n chessboard.

Coloring A Border

medium
You are given an m x n integer matrix grid, and three integers row, col, and color.

Count Islands With Total Value Divisible by K

medium
You are given an m x n matrix grid and a positive integer k.

Count Nodes With the Highest Score

medium
There is a binary tree rooted at 0 consisting of n nodes.

Count Pairs of Connectable Servers in a Weighted Tree Network

medium
You are given an unrooted weighted tree with n vertices representing servers numbered from 0 to n - 1, an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional...

Count Servers that Communicate

medium
You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server.

Count Sub Islands

medium
You are given two m x n binary matrices grid1 and grid2 containing only 0's (representing water) and 1's (representing land).

Count the Number of Complete Components

medium
You are given an integer n.

Count the Number of Good Nodes

medium
There is an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0.

Count Unreachable Pairs of Nodes in an Undirected Graph

medium
You are given an integer n.

Course Schedule

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule II

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Course Schedule IV

medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.

Detect Cycles in 2D Grid

medium
Given a 2D array of characters grid of size m x n, you need to find if there exists any cycle consisting of the same value in grid.

Detonate the Maximum Bombs

medium
You are given a list of bombs.

Disconnect Path in a Binary Matrix by at Most One Flip

medium
You are given a 0-indexed m x n binary matrix grid.

Evaluate Division

medium
You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i].

Find All Groups of Farmland

medium
You are given a 0-indexed m x n binary matrix land where a 0 represents a hectare of forested land and a 1 represents a hectare of farmland.

Find Closest Node to Given Two Nodes

medium
You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.

Find Eventual Safe States

medium
There is a directed graph of n nodes with each node labeled from 0 to n - 1.

Find Subtree Sizes After Changes

medium
You are given a tree rooted at node 0 that consists of n nodes numbered from 0 to n - 1.

Flower Planting With No Adjacent

medium
You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi.

Is Graph Bipartite?

medium
There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.

Jump Game III

medium
Given an array of non-negative integers arr, you are initially positioned at start index of the array.

Keys and Rooms

medium
There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0.

Lexicographical Numbers

medium
Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.

Lexicographically Smallest String After Applying Operations

medium
You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.

Longest Absolute File Path

medium
Suppose we have a file system that stores both files and directories.

Loud and Rich

medium
There is a group of n people labeled from 0 to n - 1 where each person has a different amount of money and a different level of quietness.

Max Area of Island

medium
You are given an m x n binary matrix grid.