//considering node who's cousins need to be find as start..
node * a[10];
while(root!=start)
{
a[i]=root;
i++;
if(root->data> start->data)
root=root->left;
else
root=root->right;
}//we can do this using recursionnode *grand=a[i-2]; if(start->data> grand->data) print(grand->left->left->data,gramd->left->right->data) else print............................... I may be wrong I am new to coding.. On Monday, 21 May 2012 21:44:56 UTC+5:30, mithun wrote: > > Shouldn't having 2 queues one storing the node and other corresponding > level should be sufficient and do level order traversal? > > -mithun > > > > > On Mon, May 21, 2012 at 5:54 PM, Ashish Goel <[email protected]> wrote: > >> 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 <[email protected]> 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 <[email protected]>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 [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 view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/qqG_yzwRUhgJ. 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.
