Practice/Microsoft/Leetcode 2385. Amount of Time for Binary Tree to Be Infected
CodingOptional
You are modeling the spread of a virus through a network represented as a binary tree. Each node in the tree represents a computer in the network. When a computer becomes infected, it spreads the virus to all directly connected computers (its parent and both children, if they exist) in exactly one minute.
Given the root of a binary tree and the value of the initially infected node (start), determine how many minutes it will take for every computer in the network to become infected.
Example 1:
Input: root = [1,2,3,4,5], start = 2 Output: 2 Explanation: Minute 0: Node 2 is infected Minute 1: Nodes 1, 4, and 5 become infected (adjacent to node 2) Minute 2: Node 3 becomes infected (adjacent to node 1) Total time: 2 minutes
Example 2:
Input: root = [1], start = 1 Output: 0 Explanation: The tree has only one node which is already infected at the start.
Example 3:
Input: root = [1,2,3,4,5,6,7], start = 7 Output: 3 Explanation: Minute 0: Node 7 is infected Minute 1: Node 3 becomes infected Minute 2: Nodes 1 and 6 become infected Minute 3: Nodes 2, 4, and 5 become infected Total time: 3 minutes