implement algorithm of getLCA (no testing)