Skip to content

Commit 18eed56

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 419cccc commit 18eed56

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

data_structures/binary_tree/lowest_common_ancestor.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ def swap(a: int, b: int) -> tuple[int, int]:
2222
def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
2323
"""
2424
Create a sparse table that saves each node's 2^i-th parent.
25-
25+
2626
The given ``parent`` table should have the direct parent of each node in row 0.
2727
This function fills in:
28-
28+
2929
parent[j][i] = parent[j - 1][parent[j - 1][i]]
30-
30+
3131
for each j where 2^j is less than max_node.
32-
32+
3333
For example, consider a small tree where:
3434
- Node 1 is the root (its parent is 0),
3535
- Nodes 2 and 3 have parent 1.
3636
3737
We set up the parent table for only two levels (row 0 and row 1)
3838
for max_node = 3. (Note that in practice the table has many rows.)
39-
39+
4040
>>> parent0 = [0, 0, 1, 1] # 0 is unused; node1's parent=0, nodes 2 and 3's parent=1.
4141
>>> parent1 = [0, 0, 0, 0]
4242
>>> parent = [parent0, parent1]
@@ -58,11 +58,11 @@ def lowest_common_ancestor(
5858
"""
5959
Return the lowest common ancestor (LCA) of nodes u and v in a tree.
6060
<<<<<<< HEAD
61-
61+
6262
The lists ``level`` and ``parent`` must be precomputed. ``level[i]`` is the depth
6363
of node i, and ``parent`` is a sparse table where parent[0][i] is the direct parent
6464
of node i.
65-
65+
6666
=======
6767
6868
The lists `level` and `parent` must be precomputed. `level[i]` is the depth of node i,
@@ -107,10 +107,10 @@ def breadth_first_search(
107107
) -> tuple[list[int], list[list[int]]]:
108108
"""
109109
Run a breadth-first search (BFS) from the root node of the tree.
110-
110+
111111
This sets each node's direct parent (stored in parent[0]) and calculates the
112112
depth (level) of each node from the root.
113-
113+
114114
>>> # Consider a simple tree:
115115
>>> # 1
116116
>>> # / \\
@@ -141,18 +141,18 @@ def main() -> None:
141141
"""
142142
Run a BFS to set node depths and parents in a sample tree, then create the
143143
sparse table and compute several lowest common ancestors.
144-
144+
145145
The sample tree used is:
146146
<<<<<<< HEAD
147-
147+
148148
1
149-
/ | \
149+
/ | \
150150
2 3 4
151151
/ / \\ \\
152152
5 6 7 8
153153
/ \\ | / \\
154154
9 10 11 12 13
155-
155+
156156
=======
157157
158158
1
@@ -171,7 +171,7 @@ def main() -> None:
171171
- LCA(6, 7) --> 3
172172
- LCA(4, 12) --> 4
173173
- LCA(8, 8) --> 8
174-
174+
175175
To test main() without it printing to the console, we capture the output.
176176
177177
>>> import sys

0 commit comments

Comments
 (0)