diff options
author | terminaldweller <devi@terminaldweller.com> | 2024-01-11 00:31:06 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2024-01-11 00:31:06 +0000 |
commit | bd3983dd6bfbe76bcee4c8c771d49dd0c05ea3d5 (patch) | |
tree | 12aabcd11ced848e0878d40a5fee5fd805c602ad /2385 | |
parent | 872 (diff) | |
download | leetcode-bd3983dd6bfbe76bcee4c8c771d49dd0c05ea3d5.tar.gz leetcode-bd3983dd6bfbe76bcee4c8c771d49dd0c05ea3d5.zip |
2385
Diffstat (limited to '2385')
-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() |