Advertisement

Amount of Time for Binary Tree to Be Infected - LeetCode 2385 Solution

Amount of Time for Binary Tree to Be Infected - Complete Solution Guide

Amount of Time for Binary Tree to Be Infected is LeetCode problem 2385, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given the root of a binary tree with unique values, and an integer start . At minute 0 , an infection starts from the node with value start . Each minute, a node becomes infected if: The node is currently uninfected. The node is adjacent to an infected node. Return the number of minutes needed for the entire tree to be infected. Example 1: Input: root = [1,5,3,null,4,10,6,9,2], start = 3 Output: 4 Explanation: The following nodes are infected during: - Minute 0: Node 3 - Minute 1: Nodes

Detailed Explanation

The problem asks us to find the amount of time it takes for a binary tree to be completely infected, starting from a given node. The infection spreads to adjacent nodes (parent, left child, right child) in each minute. We need to return the number of minutes required for the entire tree to be infected. The tree has unique node values, and the 'start' node is guaranteed to exist.

Solution Approach

The solution converts the binary tree into an undirected graph represented by an adjacency list. Then, it performs a Breadth-First Search (BFS) starting from the given 'start' node. The BFS algorithm explores the graph level by level. Each level represents one minute of infection spread. The algorithm keeps track of visited nodes to avoid revisiting them. The number of levels traversed during BFS represents the total time required to infect the entire tree.

Step-by-Step Algorithm

  1. Step 1: **Convert Binary Tree to Graph:** Traverse the binary tree (e.g., using level-order traversal) and create an adjacency list representing an undirected graph. For each node, add edges to its left child, right child, and its parent (if applicable).
  2. Step 2: **Initialize BFS:** Create a queue for BFS and enqueue the 'start' node. Also, create a set to keep track of visited nodes to avoid cycles.
  3. Step 3: **Perform BFS:** While the queue is not empty, increment the 'time' (minutes). Process all nodes at the current level. For each node, find its neighbors in the graph and enqueue unvisited neighbors. Mark the enqueued neighbors as visited.
  4. Step 4: **Return Time:** After the BFS is complete (queue is empty), return the calculated 'time'. Since the time is incremented at the beginning of each loop, it's initialized to -1, so the final result accounts for starting at minute 0.

Key Insights

  • Insight 1: The core idea is to model the binary tree as an undirected graph. This allows us to easily traverse between parent and child nodes during the infection spread.
  • Insight 2: Breadth-First Search (BFS) is suitable for simulating the infection spread because it explores the tree level by level, representing the passage of time (minutes).
  • Insight 3: The longest path from the 'start' node in the converted graph represents the minimum time needed to infect the entire tree.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Companies

Asked at: Flipkart, Goldman Sachs, Nutanix, PhonePe, ServiceNow, ShareChat, Snap.