**Are you also facing difficulties in finding the Lowest Common Ancestor between two nodes? If yes, then we are here to help you.**

Every programmer who is studying the topic of binary trees is facing problems in understanding the term LCA.

Those who are new to programming and learning DSA may face problems understanding the concept of the binary tree. The concept involves a series of questions that need to be answered.

**Keep on reading the guide to know better about the LCA of Binary Tree and how to solve the problems related to it.**

Table of Contents

**What is LCA?**

The Lowest Common Ancestor is a problem that is performed on the Binary Tree with the motive to find the lowest common element between two given nodes.

If you don’t remember the concept of the Binary Tree, then keep on reading. The Binary Tree is a type of Tree Data Structure of Non-linear data structure. It has at most 2 children and due to this, the binary tree is divided into left and right child.

The Lowest Common Ancestor or LCA is used to find the lowest data element from the two nodes of the Binary Tree. Due to the problem statement, a lot of companies ask about the approach that is used to solve it.

Whether you are going to appear for an interview or for coding at the online contest. It is important for you to have an understanding of the questions which are related to Data Structure & Algorithms.

Some of the most asked questions of the DSA are from the topics of linear data structure which includes arrays, subarrays, and **subarray with given sum** whereas the others are from non-linear data structure which includes different problems from Binary Tree, Graphs, etc.

So, it is important for you to have proper knowledge of the concepts that are most asked in the interviews or on the coding platforms.

The LCA of Binary Tree is one of them. To make you understand the concept that it follows, how to solve it, and why should I learn LCA. We are listing some of the reasons below. Make sure to check them.

**Why should you learn about LCA?**

You might be thinking that why should I learn about LCA? The LCA or Lowest Common Ancestor is an important part of the Data Structure & Algorithm which works according to the Tree data structure.

The recruiter also evaluates how much knowledge the candidate is having about the DSA. Most companies are hiring employees who have a good understanding of the DSA.

So, it is important for you to have an understanding of it.

**How to Solve LCA of Binary Tree Problems**

For finding the LCA of a Binary Tree, you will need to make a good algorithm that works properly with all the cases. Also, you will need to check it with the help of a dry run that whether it is correct or not.

So, here we will illustrate how you can solve the LCA of Binary Tree Problems.

**Problem Statement**

Given a Binary Tree, find the LCA between the given nodes: (8,9), (1,7), (3,7)

1

| |

2 3

| | | |

4 5 6 7

| |

8 9

**Algorithm**

- We all know that the LCA is the lowest common ancestor between the two nodes. So, we have to check for the connecting node which is close to both given nodes.
- We will create a function that will be recursively used to take the node and the two different values.
- After it, we will check the values with several conditions and look whether the value lies in the right subtree or not.
- We will do the same for the left tree. Again, we will use the control statements to compare the values of the data elements that we have taken in node 1 and node 2.
- After implementing the control statements, if we don’t get any result, then we will return the current node value.

**Dry Run**

- Let’s start with the dry run for the given tree. First of all, we will find the LCA of the node (8,9). So, when we start checking by traversing, then we will see that the 8 and 9 are connected with node 6. So, here the LCA will be 6 as it is very closest to both of the nodes and less than both of them.
- Now, we will start the check for the next node (1,7). The node (1,7) is connected with the 3. However, it is greater than 1 but less than 7. So, hereafter the comparison, we will get to know that the LCA of the node (1,7) is 1 which fulfills all the conditions of the Lowest Common Ancestor,
- Let us check the LCA for the next node (3,7). First of all, we will check whether the node is interconnected with any parent node or not. After checking, we will compare the value for the node by which it is interconnected. If it is not connected with any node, then we will check the value of both nodes. When you will see, then you will get to know that (3,7) is not connected with any other node. Now, we will compare the values of both nodes. After the comparison, we can easily know that 3 is less than 7. So, we will declare it as the LCA of the node (3,7)

We have mentioned the algorithm and the dry run that will be used in it. Now, you will have to code it in your preferred language.

**Also Read: **Statistics in Data Science | Detailed Guide To Follow

**Conclusion **

**Finding the LCA of the Binary Tree is not hard if you are clear with the concept of DSA. **

Start practicing the DSA problems which are asked mostly like** **subarray with given sum, LCA of the binary tree, subarray, multiplication tree, and much more. This all will be going to help you in getting placed at the best IT companies.