Dfs
DFS
Forty questions focused on depth-first search, for exploring trees and graphs branch by branch.
Find if Path Exists in Graph
easyThere is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive).
Flood Fill
easyYou 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
easyYou 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
mediumGiven 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
mediumGiven 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
mediumYou 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
mediumGiven 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
mediumThere 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
mediumThere are n cities connected by some number of flights.
Check if There is a Valid Path in a Grid
mediumYou are given an m x n grid.
Check Knight Tour Configuration
mediumThere is a knight on an n x n chessboard.
Coloring A Border
mediumYou are given an m x n integer matrix grid, and three integers row, col, and color.
Count Islands With Total Value Divisible by K
mediumYou are given an m x n matrix grid and a positive integer k.
Count Nodes With the Highest Score
mediumThere is a binary tree rooted at 0 consisting of n nodes.
Count Pairs of Connectable Servers in a Weighted Tree Network
mediumYou 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
mediumYou 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
mediumYou 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
mediumYou are given an integer n.
Count the Number of Good Nodes
mediumThere 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
mediumYou are given an integer n.
Course Schedule
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule II
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Course Schedule IV
mediumThere are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
Detect Cycles in 2D Grid
mediumGiven 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
mediumYou are given a list of bombs.
Disconnect Path in a Binary Matrix by at Most One Flip
mediumYou are given a 0-indexed m x n binary matrix grid.
Evaluate Division
mediumYou 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
mediumYou 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
mediumYou 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
mediumThere is a directed graph of n nodes with each node labeled from 0 to n - 1.
Find Subtree Sizes After Changes
mediumYou are given a tree rooted at node 0 that consists of n nodes numbered from 0 to n - 1.
Flower Planting With No Adjacent
mediumYou 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?
mediumThere is an undirected graph with n nodes, where each node is numbered between 0 and n - 1.
Jump Game III
mediumGiven an array of non-negative integers arr, you are initially positioned at start index of the array.
Keys and Rooms
mediumThere are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0.
Lexicographical Numbers
mediumGiven an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.
Lexicographically Smallest String After Applying Operations
mediumYou 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
mediumSuppose we have a file system that stores both files and directories.
Loud and Rich
mediumThere 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
mediumYou are given an m x n binary matrix grid.