LCA of Binary Tree Problem Statement

You are given a binary tree consisting of distinct integers and two nodes, X and Y. Your task is to find and return the Lowest Common Ancestor (LCA) of these two nodes.

The LCA of X and Y is defined as the farthest node from the root that is a shared ancestor of both X and Y.

Note:
You can assume that the nodes X and Y exist in the given binary tree.

Example:

Input Format:
The first line contains an integer 'T', the number of test cases to run. For each test case: The subsequent line contains the tree's nodes in level order, with node values separated by spaces. Use -1 to denote null nodes. The second line contains two integers, 'X' and 'Y', representing the nodes for which you need to find the LCA.
Example Tree Representation:

Example Tree

Example input for this tree: 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format:
For each test case, output the value of the LCA node of the given two nodes in the binary tree. Print the output on a new line for each test case.

Constraints:

  • 1 <= T <= 100
  • 0 <= N <= 3000, where N is the number of nodes in the binary tree.
  • 0 <= DATA <= 10^4, where DATA is a node value.
  • Time Limit: 1 second
Note:
The explanation provided earlier describes how the input is structured for the tree. In the actual input, the nodes are given in a single line, separated by spaces.
AnswerBot
4mo

Find the Lowest Common Ancestor (LCA) of two nodes in a binary tree.

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

  • Use recursion to trave...read more

Help your peers!
Select
Add answer anonymously...

UnitedHealth Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. What are some SQL queries and JOIN operations you have worked with in your datab...read more
A Software Developer Intern was asked Q. Maximize Stock Trading Profit You are given an array prices, representing stock ...read more
A Software Developer Intern was asked Q. What are the differences between fragmentation and segmentation in operating sys...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. What are some SQL queries and JOIN operations you have worked with in your datab...read more
A Software Developer Intern was asked Q2. Maximize Stock Trading Profit You are given an array prices, representing stock ...read more
A Software Developer Intern was asked Q3. What are the differences between fragmentation and segmentation in operating sys...read more
UnitedHealth 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