This is a problem of findind Lowest Common Ancestor in BST.