Use a recursive function....this below function will add up all nodes at the same level.
void Traverse(Node n,int level, LinkedList list){ if(n==null) return; if(n>list.size()) list.add(n.value); else list.set(list.get(level)+n.value); Traverse(n.left,level+1,list); Traverse(n.right,level+1,list); } On Fri, Jul 30, 2010 at 12:03 AM, ashish agarwal < ashish.cooldude...@gmail.com> wrote: > I think use bfs ... > > On Thu, Jul 29, 2010 at 11:26 AM, irfan naseef <irfannase...@gmail.com>wrote: > >> >> >> On Thu, Jul 29, 2010 at 11:35 PM, ashish agarwal < >> ashish.cooldude...@gmail.com> wrote: >> >>> please explain q ..i didnt understand >>> >>> >>> On Thu, Jul 29, 2010 at 11:01 AM, irfan <irfannase...@gmail.com> wrote: >>> >>>> I attended Amazon placement test today . There was a question where i >>>> got confused.It is as follows. >>>> >>>> Give an algorithm to connect all nodes in one level of a binary tree . >>>> >>>> 5 >>>> 5 >>>> / >>>> \ / \ >>>> 8 2 --------> 8 ---- >>>> > 2 >>>> / \ / / >>>> \ / >>>> 1 2 6 1 ---> 2 -->6 >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Algorithm Geeks" group. >>>> To post to this group, send email to algoge...@googlegroups.com. >>>> To unsubscribe from this group, send email to >>>> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> >>>> . >>>> 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 algoge...@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> >>> . >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> Sorry for that. I attached a jpg file showing what shud the algo do . >> Question is that, we are given a tree. algo shud connect all the nodes >> which are in same level. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algoge...@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> >> . >> 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 algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> > . > 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 algoge...@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.