You are viewing a single comment's thread. Return to all comments →
static Node lca(Node root,int v1,int v2) { Node curr = root; while ((curr.data < v1 && curr.data < v2) || (curr.data > v1 && curr.data > v2)) { if(curr.data < v1 && curr.data < v2){ curr = curr.right; } else { curr = curr.left; } } return curr; }
Changed it to iterative so the space complexity is O(1)
Binary Search Tree : Lowest Common Ancestor
You are viewing a single comment's thread. Return to all comments →
Changed it to iterative so the space complexity is O(1)