-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2385.py
50 lines (39 loc) · 1.49 KB
/
m2385.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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:
self.output:int = 0
def infectDecendents(curr: Optional[TreeNode], time: int) -> None :
self.output = max(self.output, time)
time += 1
if curr.left :
infectDecendents(curr.left, time)
if curr.right :
infectDecendents(curr.right, time)
def infectUpwards(curr: Optional[TreeNode]) -> int :
if not curr :
return 0
if curr.val == start :
if curr.left :
infectDecendents(curr.left, 1)
if curr.right :
infectDecendents(curr.right, 1)
return 1
left = infectUpwards(curr.left)
right = infectUpwards(curr.right)
maxx = max(left, right)
if maxx == 0 :
return 0
self.output = max(self.output, maxx)
maxx += 1
if left == 0 and curr.left :
infectDecendents(curr.left, maxx)
elif right == 0 and curr.right :
infectDecendents(curr.right, maxx)
return maxx
infectUpwards(root)
return self.output