LCA in a BST

Find the least common ancestor in a Binary Search Tree.

Possible solution:
Binary search Tree is an ordered tree with the insertion logic of putting an element in the left subtree if it is smaller than the present root node else in the right subtree.
So the funda here would be to take the two values of the node and do the insertion logic till the for the two values you have to traverse in different directions. i.e. for one you need to go to the left sub tree for one of the values while for the other value you need to go to the right subtree. At this juncture lies your LCA :)
Code will soon follow. If you have code to share please do so in the response section.


Post a Comment