Dsa
Binary Search Tree
Medium
Lowest Common Ancestor in BST
Write a program to find the lowest common ancestor (LCA) of two given nodes in a binary search tree (BST). The node values will be provided in a single line, followed by the node values to find. An LCA of two nodes is defined as the lowest node that is an ancestor of both nodes. You can assume that both nodes exist in the BST.
Input Format:
- A single line containing the values of the nodes followed by the values of the two nodes to find separated by spaces.
Output Format:
- Print the value of the lowest common ancestor.
Example:
Input:
20 10 30 5 15 25 35 10 15
Output:
10
Input:
20 10 30 5 15 25 35 5 25
Output:
20
Key concepts
binary_search_treelowest_common_ancestorsearch
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now