mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
Create bst_lowest_common_ancestor.py
This commit is contained in:
parent
e3e5e4bf35
commit
fab5bed80f
1 changed files with 23 additions and 0 deletions
23
trees/bst_lowest_common_ancestor.py
Normal file
23
trees/bst_lowest_common_ancestor.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue