#2385
Amount of Time for Binary Tree to Be Infected
specialist · 625 · lc medium +29 · verified · 65.2% accepted · 3,221 likes · top 69%
Description
Given the root of a binary tree with distinct node values and an integer start, a virus begins spreading at minute 0 from the node whose value equals start. Every minute, any uninfected node adjacent to an infected node becomes infected.
Return the number of minutes until the whole tree is infected.
Example 1:
Input: root = [1,5,3,null,4,10,6,9,2], start = 3
Output: 4
Explanation: The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
Example 2:
Input: root = [1], start = 1
Output: 0
Explanation: At minute 0, the only node in the tree is infected so we return 0.
Code
1
2
3
4
5
6
7
8
9