Least Common Ancestor of Two Nodes

You are given an arbitrary binary tree with 'N' nodes, where the nodes are numbered with values in the range of integers. Given two specific nodes, 'x' and 'y', your task is to determine and print the least common ancestor (LCA) of these nodes in the tree.

The LCA is defined as the lowest node in the tree which has both 'x' and 'y' as its descendants (or the node itself being a descendant).

Input:

The input begins with an integer 'T' indicating the number of test cases.
For each test case, you will be provided with one line containing the values of the nodes of the tree in level order format (use -1 for NULL nodes).
x y (followed by the values of x and y for which LCA is to be found)

Output:

Output the LCA of the given nodes for each test case on a new line.

Example:

Input:
1
3 5 1 -1 -1 0 8
5 1
Output:
3
Explanation:

In the provided binary tree, the node 3 is the least common ancestor of 5 and 1.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 10^4
  • 1 <= nodeVal <= 10^9
  • Time Limit: 1 sec

Note:

Ensure that one or both nodes x and y may not be present in the tree, and you must handle references accordingly when they are NULL.

AnswerBot
4mo

Find the least common ancestor of two nodes in a binary tree.

  • Traverse the tree to find the paths from the root to each node, then compare the paths to find the LCA.

  • Use recursion to traverse the tree e...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. Given a matrix, find the shortest distance between two given points located anyw...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 4mo agoQ1. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ2. Given a matrix, find the shortest distance between two given points located anyw...read more
A Software Developer Intern was asked 4mo agoQ3. For a given array, how would you count the number of inversions?
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits