Tell me if this Pseudo code Works.
Initial conditions tp be checked are:
node * leftmostRightCousin(node *root,node *p)
{
if(root==p || root->left==p || root->right==p) // in dis
case der cant be any cousins for any node at first level.
return NULL;
while(root)
{
// perform level order traversal the tree and at each
level check if the node *p exists at each level.
// If it exists then check all sucessive nodes in that level
after node *p are having same parent as node *p.
// If same parent dey r brothers.proceed to next node at same
leve.
// continue dis until u find a node in dat level in which p is
present and u get a node which does nt have same parent as that of p. that
is the leftmost right cousin of p.
}
}
correct me if i am wrong :)
*Muthuraj R
IV th Year , ISE
PESIT , Bangalore*
On Wed, Aug 10, 2011 at 3:28 PM, muthu raj <[email protected]> wrote:
> @Mohit : in ur code u are breaking out when there parent reaches root. But
> not every time will we have to reach root. For example: Leftmost right
> cousin of 1 is 9...in dat case parent should stop at 4. which is not the
> root.
>
> *Muthuraj R
> IV th Year , ISE
> PESIT , Bangalore*
>
>
>
> On Wed, Aug 10, 2011 at 3:26 PM, muthu raj <[email protected]> wrote:
>
>> @coder dumca: If u apply DFS u will get a brother and not cousin. And yes
>> it works only for some cases.
>>
>> *Muthuraj R
>> IV th Year , ISE
>> PESIT , Bangalore*
>>
>>
>>
>> On Wed, Aug 10, 2011 at 2:24 PM, coder dumca <[email protected]>wrote:
>>
>>> apply BFS
>>> the node after the particular node(whode cousin to be find) will be the
>>> required node
>>>
>>> On Wed, Aug 10, 2011 at 2:15 PM, Puneet Chawla <
>>> [email protected]> wrote:
>>>
>>>>
>>>> Agree with mohit goel..
>>>>
>>>> On Wed, Aug 10, 2011 at 11:22 AM, Mohit Goel <[email protected]
>>>> > wrote:
>>>>
>>>>> 10
>>>>> 4 5
>>>>> 2 7 6 11
>>>>> 1 3 9 8 12 13 14 15
>>>>>
>>>>>
>>>>> i think we should first find the parent of the particular node ..then
>>>>> apply the concept as told by Brijesh on it ....
>>>>>
>>>>> p =parent(q);
>>>>> r = parent(p);
>>>>> count =1;
>>>>> while(p ==isright(r))
>>>>> {
>>>>> p=r;
>>>>> r=parent(r);
>>>>> count++;
>>>>> if(r==root)
>>>>> break;
>>>>>
>>>>> }
>>>>>
>>>>> if(d =right(r))
>>>>> {
>>>>> while(count!=0)
>>>>> {
>>>>> if(d->left)
>>>>> d=d->left;
>>>>> else d=d->right;
>>>>> count--;
>>>>> }
>>>>> }
>>>>> else return NULL;
>>>>> o/p=d->value;
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Algorithm Geeks" group.
>>>>> To post to this group, send email to [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected].
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> With regards
>>>> ............
>>>> Puneet Chawla
>>>> Computer Engineering Student
>>>> NIT Kurukshetra
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.