From bd3983dd6bfbe76bcee4c8c771d49dd0c05ea3d5 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 10 Jan 2024 19:31:06 -0500 Subject: 2385 --- 2385/main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 2385/main.py diff --git a/2385/main.py b/2385/main.py new file mode 100755 index 0000000..0feaae2 --- /dev/null +++ b/2385/main.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +import typing + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.right = right + self.left = left + + +class Solution: + def amountOfTime(self, root: typing.Optional[TreeNode], start: int) -> int: + def dfs(node): + if node is None: + return + if node.left: + graph[node.val].append(node.left.val) + graph[node.left.val].append(node.val) + if node.right: + graph[node.val].append(node.right.val) + graph[node.right.val].append(node.val) + dfs(node.left) + dfs(node.right) + + graph = defaultdict(list) + dfs(root) + visited = set() + queue = deque([start]) + time = -1 + while queue: + time += 1 + for _ in range(len(queue)): + current_node = queue.popleft() + visited.add(current_node) + for neighbor in graph[current_node]: + if neighbor not in visited: + queue.append(neighbor) + return time + + +def main() -> None: + pass + + +if __name__ == "__main__": + main() -- cgit v1.2.3