leetcode 2385. Amount of Time for Binary Tree to Be Infected （python）

·  阅读 792

描述

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 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.

Example 2:

``````Input: root = [1], start = 1
Output: 0
Explanation: At minute 0, the only node in the tree is infected so we return 0.

Note:

``````The number of nodes in the tree is in the range [1, 10^5].
1 <= Node.val <= 10^5
Each node has a unique value.
A node with a value of start exists in the tree.

解析

• 该节点当前未受感染。
• 该节点与受感染的节点相邻。

解答

``````class Solution(object):
def __init__(self):
self.parent = {}
self.child = collections.defaultdict(list)
def amountOfTime(self, root, start):
def dfs(root):
if not root:
return
if root.left:
self.parent[root.left.val] = root
self.child[root.val].append(root.left)
dfs(root.left)
if root.right:
self.parent[root.right.val] = root
self.child[root.val].append(root.right)
dfs(root.right)
dfs(root)
result = 0
if start == root.val and not self.parent and not self.child:
return result
stack = [start]
visited = set()
while stack:
for _ in range(len(stack)):
node = stack.pop(0)
if node in self.parent and self.parent[node].val not in visited:
stack.append(self.parent[node].val)
for c in self.child[node]:
if c.val not in visited:
stack.append(c.val)
if stack:
result += 1
return result

运行结果

``````80 / 80 test cases passed.
Status: Accepted
Runtime: 2872 ms
Memory Usage: 176.1 MB

原题链接

leetcode.com/contest/wee…