Hi Guys,
I am stumbled by this question. The following is my approach:
Lets say the two nodes are node1 and node2
For any node (lets say node1), find the path from Root to
node1 and store it in an HashMap
For the other node node2, find the path from root to node2, and while traversing back,
check if any of the nodes are present in the hashmap or not
Return the first found node
Time Complexity is O(n) and Space Complexity is also O(h), where n is the height of the tree
I just wanted to know how good is this approach. Or whether there exists any other better solution.
EDIT: The given tree is Binary Tree and not BST. So, the time taken to find a node is linear to the size of nodes.