Create bst_lowest_common_ancestor.py

This commit is contained in:
marina 2023-08-03 15:26:35 -07:00 committed by GitHub
parent e3e5e4bf35
commit fab5bed80f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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