Binary Search Tree : Lowest Common Ancestor

  • + 0 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)