Find the Largest Common Ancestor Problem
Given a binary search tree and two distinct nodes within it, the task is to determine their largest common ancestor. This ancestor is defined as the common ancestor with the highest value among all shared ancestors of the given nodes. It is guaranteed that both nodes are present in the binary tree.
Input:
The first line of the input contains an integer 'T' indicating the number of test cases.
Each test case consists of multiple lines:
1. The first line contains elements of the tree in level order, with node values separated by a single space. A placeholder of -1 is used for null nodes.
2. The second line contains the data for Node 1.
3. The third line contains the data for Node 2.
Output:
For each test case, print an integer in a single line, which is the largest common ancestor of the provided nodes.
Example:
Example input for tree:
20 10 35 5 15 30 42 -1 13 -1 -1 -1 -1 -1 -1 -1
Node 1: 10
Node 2: 35
Example output:
20
Constraints:
- 1 <= T <= 5
- 1 <= N <= 105
- 0 <= data <= 109
- Time limit: 1 second
Note:
You are not required to print anything explicitly as it has already been managed. Simply implement the required function to return the answer. Use level order traversal input for constructing the tree.

AnswerBot
4mo
Find the largest common ancestor of two nodes in a binary search tree.
Implement a function to find the largest common ancestor of two nodes in a binary search tree.
Use level order traversal input to c...read more
Help your peers!
Add answer anonymously...
Microsoft Corporation Software Developer Intern interview questions & answers
A Software Developer Intern was asked 7mo agoQ. What were your responsibilities during your internship?
A Software Developer Intern was asked 7mo agoQ. What is the difference between polymorphism and inheritance?
A Software Developer Intern was asked 9mo agoQ. Given a string, reverse the order of characters using standard data structures a...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked 6mo agoQ1. What were your responsibilities during your internship?
A Software Developer Intern was asked 6mo agoQ2. What is the difference between polymorphism and inheritance?
A Software Developer Intern was asked 8mo agoQ3. Given a string, reverse the order of characters using standard data structures a...read more
>
Microsoft Corporation Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

