New update is available. Click here to update.

Last Updated: 24 Mar, 2021

Moderate

```
Let βTREEβ be a binary tree. The lowest common ancestor of two nodes, βN1β and βN2β, is defined as the lowest node in βTREEβ with βN1β and βN2β as descendants (where we allow a node to be a descendant of itself).
```

```
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two single space-separated integers, βN1β and βN2β, representing the nodes for which LCA has to be found.
The second line of each test case contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is βNULLβ, we take -1 in its place.
```

```
The input for the tree depicted in the below image would be :
```

```
7
1 4
3 2 8 5
-1 -1 -1 -1 -1 -1 -1 -1
```

```
Level 1 :
The root node of the tree is 7
Level 2 :
Left child of 7 = 1
Right child of 7 = 4
Level 3 :
Left child of 1 = 3
Right child of 1 = 2
Left child of 4 = 8
Right child of 4 = 5
Level 4 :
Left child of 3 = NULL(-1)
Right child of 3 = NULL(-1)
Left child of 2 = NULL(-1)
Right child of 2 = NULL(-1)
Left child of 8 = NULL(-1)
Right child of 8 = NULL(-1)
Left child of 5 = NULL(-1)
Right child of 5 = NULL(-1)
The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are βNULLβ(-1).
```

```
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
7 1 4 3 2 8 5 -1 -1 -1 -1 -1 -1 -1 -1
```

```
For each test case, print a single line containing a single integer denoting the LCA of given nodes N1 and N2.
The output for each test case is printed in a separate line.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 100
1 <= N <= 10 ^ 4
1 <= DATA <= 10 ^ 4
N1 != N2
N1 and N2 exist in the βTREEβ.
The βTREEβ contains unique nodes.
Where βTβ is the number of test cases, βNβ is the number of nodes in the βTREEβ, βDATAβ represents the value of the node, βN1β and βN2β represent the nodes of which LCA has to be found.
Time limit: 1 sec.
```

Approaches

For both the nodes, the idea is to use the βparentβ pointer to traverse up in the tree and keep track of their ancestors.

Firstly, for the node βn1β, we will traverse up in the tree till the root node and store all of its ancestors in a hash set. Then, we will traverse the tree using βn2β. While traversing, we will check whether βn2β or any of its ancestors is present in the hash set. The first node to be found in the hash set will be LCA of βn1β and βn2β.

Algorithm:

- Create a hash set, βhashSetβ to store the ancestors of node βn1β.
- Run a loop while βn1β is not βNULLβ.
- Insert the data of node βn1β in the βhashSetβ.
- Update βn1β by its parent.

- Again run a loop while βn2β is not βNULLβ.
- If hashSet.count(n2->data) != 0 that shows that data of node βn2β is already present in βhashSetβ. So, return node βn2β.
- Else update βn2β by its parent.

- Return βNULLβ, which indicates that LCA was not found.