Leetcode Problem 2368. Reachable Nodes With Restrictions

2368. Reachable Nodes With Restrictions

Leetcode Solutions

Breadth First Search (BFS)

  1. Create a boolean array seen to keep track of visited nodes, initializing all entries to false.
  2. Mark all restricted nodes as visited in the seen array.
  3. Initialize a queue and enqueue the starting node 0, marking it as visited.
  4. Set ans to 0, which will keep count of the reachable nodes.
  5. While the queue is not empty: a. Dequeue the front node curr_node from the queue. b. Increment ans by 1. c. For each neighbor of curr_node, if it is not visited, mark it as visited and enqueue it.
  6. After the queue is empty, return ans as the result.
UML Thumbnail

Depth First Search (DFS): Recursive

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...