1
# Runtime: 3032 ms (Top 5.02%) | Memory: 139.1 MB (Top 14.52%)3
def closestMeetingNode(self, edges: List[int], node1: int, node2: int) -> int:7
def dfs(node, arr, counter=0):9
# making sure we haven't visited the node before (i.e., value in the array != -1)10
while arr[node] == -1 and node != -1:12
# assigning how many moves it takes to reach node14
next_node = edges[node]16
# going through each neighbor if exists and updating the counter17
dfs(edges[node], arr, counter + 1)21
# find moves to reach nodes from node122
n1 = [-1 for i in range(len(edges))]25
# find moves to reach nodes from node226
n2 = [-1 for i in range(len(edges))]31
for i in range(len(edges)):33
# check if the end node is reachable from both starting nodes34
if n1[i] != -1 and n2[i] != -1:35
maximum_distance = max(n1[i], n2[i])37
# update the distance and the final answer if relevant38
if maximum_distance < res:39
res = maximum_distance