Lowest Common Ancestor of a BST Problem

Lowest Common Ancestor of a BST Problem — ExecCode Medium DSA Practice

Solve the Lowest Common Ancestor of a BST problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given a binary search tree, find the lowest common ancestor (LCA) of two given nodes p and q. For Lowest Common Ancestor of a BST, focus on the exact input variables, return only the requested value, and preserve the required time complexity for the intended pattern.

Examples

Input root = [6, 2, 8, 0, 4, 7, 9], p = 2, q = 8; Output 6

Constraints

2 ≤ nodes ≤ 10^5 All values unique p ≠ q p and q exist

Practice Lowest Common Ancestor of a BST free on ExecCode. Browse DSA problems, topic map, and placement guides.