Amount of Time for Binary Tree to Be Infected

MediumTreesbfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the root of a binary tree with distinct values and an integer start. At minute 0 an infection begins at the node whose value is start. Each minute, every uninfected node that is adjacent to an infected node becomes infected. Two nodes are adjacent when one is the parent of the other. Return the number of minutes until every node in the tree is infected.

Examples

Input: root = [1,5,3,null,4,10,6,9,2], start = 3
Output: 4
Minute 1 reaches 1, 10, 6; minute 2 reaches 5; minute 3 reaches 4; minute 4 reaches 9 and 2.
Input: root = [1], start = 1
Output: 0
The only node is already infected.

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^5
  • All values are distinct
  • `start` is the value of a node in the tree

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bfs problems