java - find path between root and multiple of 5 -


i facing hard time implement program finding path node b-tree root multiple of 5.

example:

     12     /  \    4    7   /\    /\  5  3  4 10 

consider tree. program should print

12 -> 4 -> 5  12 -> 7 -> 10 

edit:

yes have tried , following algo following: traverse in-order , compare values multiple of 5. if is, start adding nodes in linkedlist , return list back. approach works if have 1 multiple of 5. if there more multiples, fails.

following have tried:

linkedlist<integer> getpaths(node parent, int multiple){      if(parent == null)         return null;      linkedlist list = new linkedlist();     list = getpaths(parent.getleftchild(), 5);       if(parent.getsid() % multiple == 0){         list.add(parent.getsid());         return list;     }      list = getpaths(parent.getrightchild(),5);      if(list != null)         list.add(parent.getsid());      return list;  } 

the problem is, when do:

list = getpaths(parent.getrightchild(), 5); 

you overwrite value returned here:

list = getpaths(parent.getleftchild(), 5); 

you need linkedlist<linkedlist<integer>> store paths.

either global list:

linkedlist<linkedlist<integer>> globallist; linkedlist<integer> getpaths(node parent, int multiple) 

or passing around list:

linkedlist<integer> getpaths(node parent, int multiple,                              linkedlist<linkedlist<integer>> biglist) 

getting work require quite bit of changes current program, you'll need copy lists @ points , switch around order in populate list (change populating bottom-up top-down).


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -