VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-time-required-to-infect-all-the-nodes-of-binary-tree/

⇱ Minimum time required to infect all the nodes of Binary tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum time required to infect all the nodes of Binary tree

Last Updated : 23 Jul, 2025

Given a binary tree and a node start that is initially infected. For every second, neighbours of an infected node get infected. The task is to find the minimum time in which the whole tree will be infected.

Examples:

Input:
         10
      /     \
 12      13
        /       \
14        15
   /  \        /   \
21 22  23  24
Start = 14
Output: 3

Input: 
       3
    /    \
  4      1
            \
             2
Start = 1
Output: 2

An approach using BFS (Breadth-first search):

The idea to solve this problem is by doing the BFS (breadth-first search) algorithm on the tree,  

Starts from the given special node "start". During the BFS make all adjacent node infected. Keep counting the time in result, where we have visited all the node. 

One problem while infecting the adjacent node is that we can easily know the node's children (left child or right child) which is adjacent but we can't know that node's parent directly. So, to overcome this problem we have to generate a parent-node relationship into some data structure, there we can keep track to node's parent.

Follow the steps below to implement the above idea:

  • Store parent-child relations for each node in the parent array. (i.e, keeping track of the parent of any node)
  • Find the actual node "node" of a given special node start in the tree.
  • Create a queue for performing the BFS and an array visited to keep track of which node has already been infected.
  • Do BFS (Breadth-first search) by initially storing the "node" in the queue and making this "node" visited.
  • Do the following while queue size is greater than zero.
    • Iterate over the size of the queue and do the following:
      • Check if the current node's parent exists in the tree and is not infected yet.
        • Push into the queue and make it visited to show it's infected now.
      • Check if the current node's left child exists in the tree and is not infected yet.
        • Push into the queue and make it visited to show it's infected now.
      • Check if the current node's right exists in the tree and is not infected yet.
        • Push into the queue and make it visited to show it's infected now.
    • Increment the result by 1. (i.e, the infection has already spread by the above steps for this time.)
  • Return the result.

Below is the implementation of the above approach.


Output
3

Time Complexity: O(N), where N is the number of nodes in the binary tree.
Auxiliary Space: O(N)

Another method (Using Depth-first search (DFS))

The idea to solve this problem is by doing the  Depth-first search (DFS)  algorithm on the tree,

Algorithm

  • Create a function called "findParent" that takes a root node, a parent node pointer, a vector of Node pointers called "parent", and an integer called "start".
    • If the root node is NULL, return.
    • Store the parent of the current node in the "parent" vector at the index of the current node's value.
    • If the current node's value equals "start", set the global "node" variable to the current node.
    • Recursively call the "findParent" function with the left and right children of the current node, passing in the current node as the parent node pointer.
  • Create a function called "amountOfTime" that takes a root node and an integer called "start".
    • Create a "parent" vector of Node pointers with size 100005 and initialize all elements to NULL.
    • Call the "findParent" function with the root node, NULL as the parent node pointer, the "parent" vector, and the "start" integer.
    • Create a "visited" vector of bools with size 100005 and initialize all elements to false.
    • Declare an integer variable called "result" and initialize it to 0.
  • Define a lambda function called "dfs" that takes a Node pointer called "curr" and an integer called "dist".
    • Mark the current node as visited in the "visited" vector.
    • Update the "result" variable to be the maximum of "result" and "dist".
    • Traverse the current node's left child, right child, and parent (if it exists) using a range-based for loop.
    • If a child or parent node is found and has not been visited before, recursively call the "dfs" function with the child/parent node and the updated distance.
    • Call the "dfs" function with the global "node" variable and 0 as the distance.
    • Return the "result" variable.

Output
3

Time complexity: O(N),where N is the number of nodes in the binary tree, as it visits each node once.  
Space complexity:  O(N),because it uses a vector of Node pointers called "parent" with size 100005 to store the parent nodes of each node in the binary tree.

Comment