<p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>

<p>Each minute, a node becomes infected if:</p>

<ul>
	<li>The node is currently uninfected.</li>
	<li>The node is adjacent to an infected node.</li>
</ul>

<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- 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
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
	<li>Each node has a <strong>unique</strong> value.</li>
	<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>

# 二叉树中节点的值 互不相同 。另给你一个整数 start 。在第 0 分钟，感染 将会从值为 start 的节点开始爆发。

# 每分钟，如果节点满足以下全部条件，就会被感染：
# 节点此前还没有感染。
# 节点与一个已感染节点相邻。

# 返回感染整棵树需要的分钟数。

# 解题思路：
# 这个问题可以看作是一个图遍历问题，树中的每个节点可以看作是图中的一个节点，树中的每条边（即父节点与子节点之间的连接）可以看作是图中的边。
# 我们的目标是计算从指定的起始节点开始，感染整个树所需的时间。

# 主要步骤如下：

# 构建父节点映射：首先需要构建每个节点的父节点映射，这样我们就可以在遍历过程中方便地从当前节点移动到它的父节点。

# 广度优先搜索（BFS）：从起始节点开始，使用广度优先搜索（BFS）遍历树。
# BFS 是按层次遍历节点的，因此很适合用来计算感染每一层节点所需的时间。每一步我们都会将相邻的未感染节点（包括左右子节点和父节点）加入队列。

# 计算所需时间：每次遍历一层节点时，增加时间计数器。
# 当队列为空时，遍历结束，当前时间计数器的值即为感染整个树所需的时间。

# deque([(node_start, 0)])
# 從 start node 開始 -> 往 node.left, node.right, parent 找 nodes
# dq.append((node, time + 1))

# recursive build parent mode and node_parent_dict
# defaultdict {node.val: parent}
# {node.left, node} {node.right, node}
# def buildParent(node, parent):
    # if node:
    #     node_parent_dict[node] = parent
    #     buildParent(node.left, node)
    #     buildParent(node.right, node)

# visited = set()

# max_time = max(max_time, time)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def amountOfTime(self, root: Optional[TreeNode], start: int) -> int:
        node_parent_dict = defaultdict(TreeNode)
        self.start_node = TreeNode()
        self.found = False

        def buildParent(node, parent):
            if node:
                if not self.found and node.val == start:
                    self.start_node = node
                    self.found = True
                node_parent_dict[node] = parent
                buildParent(node.left, node)
                buildParent(node.right, node)

        buildParent(root, None)

        visited = {self.start_node}
        dq = deque([(self.start_node, 0)])
        max_time = 0        

        while dq:
            cur_node, time = dq.popleft()
            max_time = max(max_time, time)

            for neighbor in (cur_node.left, cur_node.right, node_parent_dict[cur_node]):
                if neighbor and neighbor not in visited:
                    visited.add(neighbor)
                    dq.append((neighbor, time + 1))

        return max_time
331