Last Updated: 24 Mar, 2021
##### Lowest Common Ancestor of a Binary Tree III
Moderate
Problem statement

#### Note:

``````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).
``````

#### Input Format:

``````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.
``````

#### For example:

``````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
``````

#### Explanation :

``````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).
``````
##### Note :
``````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
``````
##### Output Format :
``````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.
``````

#### Note:

``````You do not need to print anything, it has already been taken care of. Just implement the given function.
``````
##### Constraints :
``````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

## 01Approach

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.