#6 most askedVery commonMediumBST
Lowest Common Ancestor of a BST
New to BST? Learn it from scratch firstBinary Search Trees in Depth โ
๐ข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.In a BST, return the value of the lowest node that has both p and q (values) as descendants. A node counts as its own descendant.
BST [6, 2, 8, 0, 4, 7, 9], p = 2, q = 8 โ 6; p = 2, q = 4 โ 2.
๐งช Practice
Write your solution, then press Run tests. Explain your approach out loud as you go.
def lca_bst(root, p, q):
pass
Output appears here. Press Run or Ctrl/โ + Enter.
Try it first. Struggling for a few minutes is how it sticks.