From 2c8a702116b82a4f37d16ef979ac89d81004b11d Mon Sep 17 00:00:00 2001 From: Dylan Buchi Date: Sat, 23 Oct 2021 20:59:27 -0300 Subject: [PATCH] [mypy] Fix type annotations in lowest_common_ancestor --- .../binary_tree/lowest_common_ancestor.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/data_structures/binary_tree/lowest_common_ancestor.py b/data_structures/binary_tree/lowest_common_ancestor.py index 2f1e893fcf99..548e704cb7c8 100644 --- a/data_structures/binary_tree/lowest_common_ancestor.py +++ b/data_structures/binary_tree/lowest_common_ancestor.py @@ -3,7 +3,7 @@ from __future__ import annotations -import queue +from queue import Queue def swap(a: int, b: int) -> tuple[int, int]: @@ -37,7 +37,7 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]: # returns lca of node u,v def lowest_common_ancestor( u: int, v: int, level: list[int], parent: list[list[int]] -) -> list[list[int]]: +) -> int: # u must be deeper in the tree than v if level[u] < level[v]: u, v = swap(u, v) @@ -61,8 +61,8 @@ def breadth_first_search( level: list[int], parent: list[list[int]], max_node: int, - graph: dict[int, int], - root=1, + graph: dict[int, list[int]], + root: int = 1, ) -> tuple[list[int], list[list[int]]]: """ sets every nodes direct parent @@ -70,7 +70,9 @@ def breadth_first_search( calculates depth of each node from root node """ level[root] = 0 - q = queue.Queue(maxsize=max_node) + + q: Queue[int] = Queue(maxsize=max_node) + q.put(root) while q.qsize() != 0: u = q.get() @@ -88,7 +90,7 @@ def main() -> None: parent = [[0 for _ in range(max_node + 10)] for _ in range(20)] # initializing with -1 which means every node is unvisited level = [-1 for _ in range(max_node + 10)] - graph = { + graph: dict[int, list[int]] = { 1: [2, 3, 4], 2: [5], 3: [6, 7],