We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

I prefer an iterative solution to this problem rather than recursive. The code is similarly brief, and there isn't any need to load up the call stack.

staticNodelca(Noderoot,intv1,intv2){Nodetemp=root;// not necessary, just use root, just a leftover from a different attempt.while(true){if(temp.data>v1&&temp.data>v2){temp=temp.left;}elseif(temp.data<v1&&temp.data<v2){temp=temp.right;}else{returntemp;}}}

## Binary Search Tree : Lowest Common Ancestor

You are viewing a single comment's thread. Return to all comments →

I prefer an iterative solution to this problem rather than recursive. The code is similarly brief, and there isn't any need to load up the call stack.

I agree. Especially considering that this is clearly tail-recursive (which makes it trivial to convert to an iterative solution).

I came with exact this solution!

awesome solution:)

This code gives the output as 1 and result is correct. In an another code output is 4 and then too result is correct.