diff options
-rwxr-xr-x | 2385/main.py | 47 |
1 files changed, 47 insertions, 0 deletions
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() |