↓Skip to main content
  1. LeetCode/

LeetCode 2385: Amount of Time for Binary Tree to Be Infected

·2 mins· ·
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-11-01

  • Submissions: Accepted (first try)

Approach
#

Infection spreads to children and to the parent, so as in LC 863, first turn the tree into an undirected graph (Tree Template 5), then BFS from start. Each level is one minute, and the time of the last level is the answer: the number of BFS levels minus one.

In other words, the answer is the distance from start to the farthest node.

My Attempts
#

Accepted on the first try.

Solution
#

class Solution:
    def amountOfTime(self, root: Optional[TreeNode], start: int) -> int:
        graph = defaultdict(list)
        queue = deque([root])
        while queue:
            node = queue.popleft()
            for child in (node.left, node.right):
                if child:
                    graph[node.val].append(child.val)
                    graph[child.val].append(node.val)
                    queue.append(child)

        minutes = -1
        seen = {start}
        queue = deque([start])
        while queue:
            minutes += 1
            for _ in range(len(queue)):
                node = queue.popleft()
                for nei in graph[node]:
                    if nei not in seen:
                        seen.add(nei)
                        queue.append(nei)
        return minutes

Building the graph with BFS instead of a recursive DFS avoids the recursion limit on deep trees (up to 10^5 nodes).

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Start minutes at -1: the first level is start itself, at time 0.
  • Searching only downward without a graph misses the direction toward the parent.

Interview Explanation
#

Infection also spreads to the parent, so I convert the tree into an undirected graph and BFS from start, one level per minute; the last level’s time is the answer, i.e. the distance from start to the farthest node. I build the graph with BFS to avoid deep recursion. Time and space O(n).

Related

LeetCode 863: All Nodes Distance K in Binary Tree
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 863 note: turn the tree into an undirected graph (adding edges to parents), then BFS k steps from target
LeetCode 314: Binary Tree Vertical Order Traversal
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Sorting Binary-Tree
LeetCode 314 note: BFS with a column index; BFS is what guarantees top-to-bottom within a column and left-to-right within a row
LeetCode 652: Find Duplicate Subtrees
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Binary-Tree
LeetCode 652 note: encode every subtree in post-order and report it on its second occurrence; string keys are O(n^2) worst case, (val, left id, right id) ids make it O(n)
LeetCode 449: Serialize and Deserialize BST
·2 mins
LeetCode Medium String Tree Depth-First-Search Breadth-First-Search Design Binary-Search-Tree Binary-Tree
LeetCode 449 note: for a BST, pre-order without null markers is enough; deserialize recursively with bounds in O(n)
LeetCode 515: Find Largest Value in Each Tree Row
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 515 note: BFS level order, taking the maximum of each level; start from -inf
LeetCode 117: Populating Next Right Pointers in Each Node II
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 117 note: connect next pointers level by level with BFS; for a non-perfect tree, looking only at the neighbour’s children breaks the chain