Dsa
Trees
Medium
Lowest Common Ancestor Finder
Given a binary search tree (BST) and two nodes, find their lowest common ancestor (LCA). The LCA is defined as the lowest node in the BST that has both nodes as descendants. You will receive the tree in level-order format (using '#' for null nodes) and the two node values to find the LCA for.
Input format:
- The first line contains a space-separated list of values representing the BST in level-order.
- The second line contains two integers, the values of the nodes to find the LCA for.
Output format:
- A single integer representing the value of the lowest common ancestor.
Example:
Input:
5 3 8 1 4 7 9
3 4
Output:
5
Key concepts
treeslowest common ancestorbinary search tree
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