Skip to content

Commit fae825f

Browse files
committed
added static typing to lowest_common_ancestor.py
1 parent 8682782 commit fae825f

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

data_structures/binary_tree/lowest_common_ancestor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
# https://en.wikipedia.org/wiki/Breadth-first_search
33

44
import queue
5+
from typing import Tuple, List, Dict
56

6-
7-
def swap(a, b):
7+
def swap(a: int, b: int) -> Tuple[int, int]:
88
a ^= b
99
b ^= a
1010
a ^= b
1111
return a, b
1212

1313

1414
# creating sparse table which saves each nodes 2^i-th parent
15-
def creatSparse(max_node, parent):
15+
def creatSparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
1616
j = 1
1717
while (1 << j) < max_node:
1818
for i in range(1, max_node + 1):
@@ -22,7 +22,7 @@ def creatSparse(max_node, parent):
2222

2323

2424
# returns lca of node u,v
25-
def LCA(u, v, level, parent):
25+
def LCA(u: int, v: int, level: List[int], parent: List[List[int]]) -> List[List[int]]:
2626
# u must be deeper in the tree than v
2727
if level[u] < level[v]:
2828
u, v = swap(u, v)
@@ -45,7 +45,7 @@ def LCA(u, v, level, parent):
4545
# sets every nodes direct parent
4646
# parent of root node is set to 0
4747
# calculates depth of each node from root node
48-
def bfs(level, parent, max_node, graph, root=1):
48+
def bfs(level: List[int], parent: List[List[int]], max_node: int, graph: Dict[int, int], root=1) -> Tuple[List[int], List[List[int]]]:
4949
level[root] = 0
5050
q = queue.Queue(maxsize=max_node)
5151
q.put(root)
@@ -59,7 +59,7 @@ def bfs(level, parent, max_node, graph, root=1):
5959
return level, parent
6060

6161

62-
def main():
62+
def main() -> None:
6363
max_node = 13
6464
# initializing with 0
6565
parent = [[0 for _ in range(max_node + 10)] for _ in range(20)]

0 commit comments

Comments
 (0)