From fab5bed80fbeab8e7148a8ab490fac1b149639ab Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Thu, 3 Aug 2023 15:26:35 -0700 Subject: [PATCH] Create bst_lowest_common_ancestor.py --- trees/bst_lowest_common_ancestor.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 trees/bst_lowest_common_ancestor.py diff --git a/trees/bst_lowest_common_ancestor.py b/trees/bst_lowest_common_ancestor.py new file mode 100644 index 0000000..1217b8a --- /dev/null +++ b/trees/bst_lowest_common_ancestor.py @@ -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