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.
Be the first one to answer
Add answer anonymously...
Top Microsoft Corporation Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Microsoft Corporation Software Developer Intern
>
Microsoft Corporation Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app