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...
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...
Depth-first search is a fundamental graph traversal algorithm. A grasp of recursion is beneficial before learning DFS. Implementing Graph Traversal with DFS Contagion Spread Problem Problem Statemnet A simulation involves N entities, each existing at a unique coordinate point. These entities functio...
A Directed Acyclic Graph (DAG) is a finite directed graph containing no directed cycles. This structure is particularly useful for modeling mathematical expressions and scheduling dependencies. Expression Representation via DAGs Mathematical or logical expressions can be efficiently represented usin...
The depth-first search algorithm explores a graph by going as far as possible along each branch before backtracking. Implemented recursively, DFS uses the call stack to manage backtracking automatically. The strategy aligns with the adage: persist untill a dead end is reached, then retreat to explor...