static Node LCA(Node root, int a, int b) {
        if (root == null)
            return null;
        if (root.getData() == a || root.getData() == b)
            return root;
        Node root1 = LCA(root.getLeft(), a, b);
        Node root2 = LCA(root.getRight(), a, b);
        if (root1 != null && root2 == null)
            return root1;
        else if (root1 == null && root2 != null)
            return root2;
        else if (root1 != null && root2 != null)
            return root;
        else
            return null;
    }


On Fri, May 17, 2013 at 9:49 AM, avinesh saini <avinesh.sa...@gmail.com>wrote:

> If one node is parent of other then Parent Node is lowest common ancestor.
> Source- http://en.wikipedia.org/wiki/Lowest_common_ancestor (just read it)
>
>
> On Mon, May 13, 2013 at 1:42 AM, rahul sharma <rahul23111...@gmail.com>wrote:
>
>> [image: BST_LCA]
>> what should be ancestor of 12 and 14.............it should be 12 or
>> 14...............if 12 then for 20 and 22 also it is 20...if 12 ans 14 has
>> ancestor as 8 then for 20 and 22 it is NULL.
>>
>> @all....plz give me clear one line definition in case if one node is
>> parent of other then what is LCA
>>
>>
>> On Sun, Apr 21, 2013 at 10:32 PM, Tushar Patil 
>> <tushar01pa...@gmail.com>wrote:
>>
>>> @rahul : It's fine solution, but can we check  the root->data == n1 ||
>>> n2 before calling function recursively, I think if we check this condition
>>> 1st it will reduce unnecessary function calls.
>>>    Correct me if i am wrong?
>>> Thanks,
>>> Tushar Patil.
>>>
>>>
>>>
>>> On Sun, Apr 21, 2013 at 10:26 PM, rahul sharma 
>>> <rahul23111...@gmail.com>wrote:
>>>
>>>> int leastCommanAncestor(struct node* root, int n1, int n2)
>>>> {
>>>>  if(root==NULL)
>>>>  return -1;
>>>>  if(root->data>n1 && root->data>n2)
>>>>  return leastCommanAncestor(root->left,n1,n2);
>>>>  else if(root->data<n1 && root->data<n2)
>>>>  return leastCommanAncestor(root->right,n1,n2);
>>>>  return root->data;
>>>>
>>>> }
>>>>
>>>> Does this code miss any case?N suppose if we have to find LCA of n1 and
>>>> n2 and suppose n1 is parent of n2 ..then this will return n1..is this fyn
>>>> or if n1 is parent of n2 then we should return -1??
>>>>
>>>> Plz. comment
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to algogeeks+unsubscr...@googlegroups.com.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>>
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Algorithm Geeks" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to algogeeks+unsubscr...@googlegroups.com.
>>
>>
>>
>
>
>
> --
> *
> *
> *regards,*
> *Avinesh Kumar
> National Institute of Technology, Calicut.*
> *Kerala- 673601*
> *+91 7849080702*
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to algogeeks+unsubscr...@googlegroups.com.
>
>
>



-- 
Mangal Dev
Ph No. 7411627654
Member Technical Staff
Oracle India Pvt Ltd
mangal....@oracle.com <mangal.d...@oracle.com>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to algogeeks+unsubscr...@googlegroups.com.


Reply via email to