mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
24 lines
503 B
Python
24 lines
503 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
def lowest_common_ancestor(root, p, q):
|
|
|
|
node, result = root, root
|
|
|
|
while node:
|
|
|
|
result = 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 result
|