Let’s see how we might implement this functionality:
First, we’ll traverse the tree starting from the root node.
Then, we’ll store the parent of each node in the dictionary until the nodes a and b are not found.
If the nodes a and b are found:
Traverse over the parents/ancestors of node a.
For each parent node, add it to the ancestors set.
Similarly, we will traverse through the ancestors of node b. If the ancestor is present in the ancestors set for a, this is the first ancestor common in between a and b (while traversing upwards), and this is the LCA node.