bool firstCousins(struct node * pRoot, struct node *pThis, (struct node*)[]
path, int pos, vector<int> firstCousins)
{
if ((!pThis) || (!pRoot)) return false;
if (pRoot->data!=pThis->data) {
  path[pos] = pRoot;
  if (!findCousins(pRoot->left, pThis, path, pos+1, firstCousins))
    return findCousins(pRoot->left, pThis, path, pos+1, firstCousins);
}
if (pos<2) return false; //this node is at level 0 or level 1
struct node* pGP = path[pos-2];
struct node *pParent = path[pos-1];
struct node *pUncle = NULL;
if (pParent == pGP->left)
{
  pUncle = pGP->right;
}else pUncle = pGP->left;
if (pUncle->left) firstCousins.add(pUncle->left->data);
if (pUncle->right) firstCousins.add(pUncle->right->data);
return true;
}


Best Regards
Ashish Goel
"Think positive and find fuel in failure"
+919985813081
+919966006652


On Mon, May 21, 2012 at 5:41 PM, Ashish Goel <ashg...@gmail.com> wrote:

> For this the cousins of 1 should be  9   8     12  13   14   15 .... how
> then can it be a 2 pass algorithm... we should also consider great
> grandparent as in this case ... Correct me if I m wrong!!
>
> the first cousins are  9,8 not 12,13...otherwise the question becomes
> really simple :)
>
> Best Regards
> Ashish Goel
> "Think positive and find fuel in failure"
> +919985813081
> +919966006652
>
>
> On Mon, May 21, 2012 at 12:54 PM, sivaviknesh <sivavikne...@gmail.com>wrote:
>
>> For this the cousins of 1 should be  9   8     12  13   14   15 .... how
>> then can it be a 2 pass algorithm... we should also consider great
>> grandparent as in this case ... Correct me if I m wrong!!
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to