Lower Common Ancestor Problem (pg 73)
Hi,
I agree 90% with the solution to this problem. However, it is not explicit what the answer would be for the LCA of nodes with value 12 and 14 in the picture. The implementation suggested would place 12 as the solution.
It seems 8 would be a more fitting LCA, so I stored the previousNode for each iteration. If the two are not both greater or both less than, then I check for if the currentNode is A or B. If so, then use previousNode (convergence). Otherwise currentNode is LCA(divergence).
Thoughts?
Erich
|