From 270783268cc48987300aa15fcadb5e3279544178 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Thu, 3 Aug 2023 15:26:48 -0700 Subject: [PATCH] add notes on lca --- trees/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/trees/README.md b/trees/README.md index 027ee5b..5bddf77 100644 --- a/trees/README.md +++ b/trees/README.md @@ -96,6 +96,36 @@ def search_bst_iterative(root, val): --- +#### find lowest common ancestor + +
+ +```python + 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 +``` + +
+ +--- + #### checking if valid