Measure Infection Time from One Valued Binary-Tree Node

Quick Overview

Measure how many minutes infection needs to reach every binary-tree node from a specified starting value through parent-child edges.

Measure Infection Time from One Valued Binary-Tree Node

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem At minute zero, the unique node with value `start` in a binary tree becomes infected. Each subsequent minute, infection spreads simultaneously across parent-child edges to uninfected neighboring nodes. Return the number of minutes until every node in the tree rooted at `root` is infected. ```cpp class Solution { public: int amountOfTime(TreeNode* root, int start); }; ``` ### Examples **Example 1:** ```text Input: root = [5,2,9, null,3], start = 3 Output: 3 Explanation: Infection reaches value 2 at minute 1, value 5 at minute 2, and value 9 at minute 3. ``` **Example 2:** ```text Input: root = [8,4,12], start = 8 Output: 1 Explanation: Both children become infected simultaneously at minute 1. ``` ### Constraints - The tree contains 1 through 100000 nodes. - Values are unique integers in [1,100000]. - A node with value start exists. ### Clarifying Questions - Is `start` a value or a node index? It is a value guaranteed to exist in the tree. - Can infection spread toward a parent? Yes, as well as toward children. - Do multiple neighbors become infected simultaneously? Yes. - What if the tree has only its starting node? Return 0. ```hint Check the boundary Trace a start at a leaf, where infection must travel toward the root before reaching another branch. ```

Overview: Measure how many minutes infection needs to reach every binary-tree node from a specified starting value through parent-child edges.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

At minute zero, the unique node with value start in a binary tree becomes infected. Each subsequent minute, infection spreads simultaneously across parent-child edges to uninfected neighboring nodes. Return the number of minutes until every node in the tree rooted at root is infected.

class Solution {
public:
    int amountOfTime(TreeNode* root, int start);
};

Examples

Example 1:

Input: root = [5,2,9, null,3], start = 3
Output: 3
Explanation: Infection reaches value 2 at minute 1, value 5 at minute 2, and value 9 at minute 3.

Example 2:

Input: root = [8,4,12], start = 8
Output: 1
Explanation: Both children become infected simultaneously at minute 1.

Constraints

  • The tree contains 1 through 100000 nodes.
  • Values are unique integers in [1,100000].
  • A node with value start exists.

Clarifying Questions Guidance

  • Is start a value or a node index? It is a value guaranteed to exist in the tree.
  • Can infection spread toward a parent? Yes, as well as toward children.
  • Do multiple neighbors become infected simultaneously? Yes.
  • What if the tree has only its starting node? Return 0.