2Brute2Best
🔥0

Amount of Time for Binary Tree to Be Infected

Medium·LeetCode #2385 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given the root of a binary tree with unique values, and an integer start. At minute 0, an infection starts at the node whose value is start.

Each minute, every uninfected node that is next to an infected node becomes infected. A node's neighbours are its left child, its right child and its parent.

Return the number of minutes until every node is infected.

Input

The root of a binary tree with unique values, and the value start of the first infected node.

Output

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

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. The whole tree is infected after 4 minutes.

Example 2
Input: root = [1], start = 1
Output: 0

The only node is infected at minute 0, so nothing else needs time.

Constraints
  • • The number of nodes is in the range [1, 10^5]
  • • 1 ≤ Node.val ≤ 10^5
  • • Each node has a unique value
  • • A node with the value `start` exists in the tree
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“infection spreads to neighbours each minute”
→ Each minute is one edge further. That is one BFS round, starting from the start node.
“neighbours include the parent”
→ In a tree the parent is a neighbour too. The tree must act as an undirected graph, so we need parent links.
“minutes until the whole tree is infected”
→ The answer is the distance to the farthest node, which is the last BFS layer.
03

Approach Complexity Ladder

Brute Force — distance from start to every nodeO(N²)O(H)

For each node, count its edges to start; the answer is the largest count

Optimal — parent map, then BFS from start, count the roundsO(N)O(N)

DFS records parents; BFS from start over left, right and parent; the answer is rounds − 1

Brute Force Intuition

A node gets infected at the minute equal to its distance from the start node. So find the distance of every node from start and return the largest. Each distance is found by climbing both root paths to where they split.

Recognition rule for this sub-pattern:

BFS reaches nodes in order of distance, so the first time you hit the target is the shortest. Add parent links to go upward.
Remember this · Tree BFS
Trigger
Level order, per-level max/average, right side view, minimum depth, nodes at distance k.
Think
Does the question care about depth / rows rather than paths?
Key question
How many nodes are in the current level (queue size at the start of the level)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.