Counting Islands in a Grid Given a 2D grid consisting of '1's (land) and '0's (water), we need to count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume all four edges of the grid are surrounded by water. Exa...
Breadth-First Search (BFS) is a graph traversal algorithm that explores nodes in layers starting from a source node. It computes the shortest path distance (d-value) and predecessor (π-value) for each node in a directed graph. The following Go code implements BFS using a adjacency list representatio...
#include <iostream> #include <queue> #include <string> using namespace std; struct Position { int row; int col; string route; }; char mazeGrid[30][50]; char directionSymbols[] = {'D', 'L', 'R', 'U'}; int moveOffsets[4][2] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}}; bool visited[30][50];...
Given the root of a binary tree, return a list where each element is the largest value in its corresponding tree level. Example 1: Input: root = [1,3,2,5,3,null,9] Output: [1,3,9] Example 2: Input: root = [1,2,3] Output: [1,3] Constraints: The number of nodes is in the range [0, 10^4]. -2^31 <= N...