Your question is Lowest Common Ancestor in BST. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
At Stripe, a service stores hierarchical configuration values in a binary search tree (BST). Given the root of a BST and two node values p and q that are guaranteed to exist in the tree, return the value of their lowest common ancestor (LCA).
The lowest common ancestor of two nodes is the deepest node that has both nodes as descendants, where a node can be a descendant of itself.
Implement a function that takes:
root: a BST represented as a nested object with keys val, left, and right, or nullp: integer value of the first nodeq: integer value of the second nodeReturn:
p and qdef lowest_common_ancestor_bst(root, p, q):