From d85fa56110ab1d54db7fc031bfbf5dc843b909ae Mon Sep 17 00:00:00 2001 From: Rodolfo P A <6721075+rodoufu@users.noreply.github.com> Date: Wed, 15 Jul 2026 15:56:28 -0300 Subject: [PATCH] Create lowest-common-ancestor-of-a-binary-search-tree.py --- ...common-ancestor-of-a-binary-search-tree.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 leetCode/tree/lowest-common-ancestor-of-a-binary-search-tree.py diff --git a/leetCode/tree/lowest-common-ancestor-of-a-binary-search-tree.py b/leetCode/tree/lowest-common-ancestor-of-a-binary-search-tree.py new file mode 100644 index 0000000..3b264fc --- /dev/null +++ b/leetCode/tree/lowest-common-ancestor-of-a-binary-search-tree.py @@ -0,0 +1,46 @@ +# https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + p_fathers = [] + q_fathers = [] + it = root + while it and it != p: + p_fathers.append(it) + if it.val > p.val: + it = it.left + else: + it = it.right + if it and it.val == p.val: + p_fathers.append(it) + + it = root + while it and it != q: + q_fathers.append(it) + if it.val > q.val: + it = it.left + else: + it = it.right + if it and it.val == q.val: + q_fathers.append(it) + + # print(f"p: {p}, q: {q}") + # print(f"p_fathers: {p_fathers}") + # print(f"q_fathers: {q_fathers}") + + i = 0 + len_p = len(p_fathers) + len_q = len(q_fathers) + while i < min(len_p, len_q): + if p_fathers[i] == q_fathers[i]: + i += 1 + else: + break + + return p_fathers[i - 1]