mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
add notes on lca
This commit is contained in:
parent
fab5bed80f
commit
270783268c
1 changed files with 30 additions and 0 deletions
|
@ -96,6 +96,36 @@ def search_bst_iterative(root, val):
|
|||
|
||||
---
|
||||
|
||||
#### find lowest common ancestor
|
||||
|
||||
<br>
|
||||
|
||||
```python
|
||||
def lca(self, root, p, q):
|
||||
|
||||
node = root
|
||||
this_lcw = root.val
|
||||
|
||||
while node:
|
||||
|
||||
this_lcw = node
|
||||
|
||||
if node.val > p.val and node.val > q.val:
|
||||
node = node.left
|
||||
|
||||
elif node.val < p.val and node.val < q.val:
|
||||
node = node.right
|
||||
|
||||
else:
|
||||
break
|
||||
|
||||
return this_lcw
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
---
|
||||
|
||||
#### checking if valid
|
||||
|
||||
<br>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue