-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfected_binary_tree.py
More file actions
36 lines (35 loc) · 1.12 KB
/
Copy pathinfected_binary_tree.py
File metadata and controls
36 lines (35 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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:
q = deque()
d = defaultdict(list)
if not root:
return []
q.append(root)
while q:
n = len(q)
for _ in range(n):
node = q.popleft()
if node.left:
q.append(node.left)
d[node.val].append(node.left.val)
d[node.left.val].append(node.val)
if node.right:
q.append(node.right)
d[node.val].append(node.right.val)
d[node.right.val].append(node.val)
ans = [0]
q = deque()
q.append((start,-1,0))
while q:
node,par ,c= q.popleft()
for ch in d[node]:
if ch!=par:
q.append((ch,node,c+1))
ans[0]=c
return ans[0]