Leetcode Problem 2385. Amount of Time for Binary Tree to Be Infected

2385. Amount of Time for Binary Tree to Be Infected

Leetcode Solutions

BFS Traversal with Parent Tracking

  1. Create a map to store the parent of each node.
  2. Perform a BFS to populate the parent map and find the start node.
  3. Initialize a queue and add the start node to it.
  4. Initialize a set to keep track of visited nodes.
  5. Initialize a variable minutes to keep track of the time passed.
  6. While the queue is not empty: a. Increment minutes. b. Process all nodes at the current level of BFS. c. For each node, add its unvisited children and parent to the queue. d. Mark the node as visited.
  7. Once the queue is empty, return minutes - 1 as the last increment happens after the last node is infected.
UML Thumbnail

DFS to Find Maximum Depth from Start Node

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...