Amount of Time for Binary Tree to Be Infected
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.
The root of a binary tree with unique values, and the value start of the first infected node.
The number of minutes until the whole tree is infected.
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.
The only node is infected at minute 0, so nothing else needs time.
- • 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
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each node, count its edges to start; the answer is the largest count
DFS records parents; BFS from start over left, right and parent; the answer is rounds − 1
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.