Skip to content

Commit 419cccc

Browse files
modify
2 parents d6fff75 + 097e9c6 commit 419cccc

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

data_structures/binary_tree/lowest_common_ancestor.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
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.
36-
36+
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.)
3939
@@ -57,11 +57,18 @@ def lowest_common_ancestor(
5757
) -> int:
5858
"""
5959
Return the lowest common ancestor (LCA) of nodes u and v in a tree.
60+
<<<<<<< HEAD
6061
6162
The lists ``level`` and ``parent`` must be precomputed. ``level[i]`` is the depth
6263
of node i, and ``parent`` is a sparse table where parent[0][i] is the direct parent
6364
of node i.
6465
66+
=======
67+
68+
The lists `level` and `parent` must be precomputed. `level[i]` is the depth of node i,
69+
and `parent` is a sparse table where parent[0][i] is the direct parent of node i.
70+
71+
>>>>>>> 097e9c6149e80f095be1b3dbef1c04ff94a7325a
6572
>>> # Consider a simple tree:
6673
>>> # 1
6774
>>> # / \\
@@ -136,6 +143,7 @@ def main() -> None:
136143
sparse table and compute several lowest common ancestors.
137144
138145
The sample tree used is:
146+
<<<<<<< HEAD
139147
140148
1
141149
/ | \
@@ -145,6 +153,17 @@ def main() -> None:
145153
/ \\ | / \\
146154
9 10 11 12 13
147155
156+
=======
157+
158+
1
159+
/ | \
160+
2 3 4
161+
/ / \\ \\
162+
5 6 7 8
163+
/ \\ | / \\
164+
9 10 11 12 13
165+
166+
>>>>>>> 097e9c6149e80f095be1b3dbef1c04ff94a7325a
148167
The expected lowest common ancestors are:
149168
- LCA(1, 3) --> 1
150169
- LCA(5, 6) --> 1
@@ -154,7 +173,7 @@ def main() -> None:
154173
- LCA(8, 8) --> 8
155174
156175
To test main() without it printing to the console, we capture the output.
157-
176+
158177
>>> import sys
159178
>>> from io import StringIO
160179
>>> backup = sys.stdout

0 commit comments

Comments
 (0)