Re: [algogeeks] Re: JAVA: Print all paths from root to leaf
@WgpShashank: actually linked list is a good choice if tree height is large.Array may not able to fill it. -- 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.
Re: [algogeeks] Re: JAVA: Print all paths from root to leaf
@don : right didn't check the flow of code correctly On Mon, Jan 30, 2012 at 9:53 PM, Don wrote: > Right idea. But you only need to remove the last item once, right at > the end of the function. > Don > > On Jan 30, 1:01 am, atul anand wrote: > > @Mihir : actually you are using linked listso you are keep on adding > > the nodes but not removing it..hence...you are getting wrong output.. > > > > i guess this should be done to fix the code. > > > > public static void paths(Node node, LinkedList list) { > >if(node == null) return; > >list.add(node.data); > > > >if(node.left == null && node.right == null) { > > print(list); > >} > >else { > > paths(node.left, list); > > > > * removeLastNodefromLinkedList();* > > paths(node.right, list); > > > >* removeLastNodefromLinkedList();* > > > >} > > > > r*emoveLastNodefromLinkedList();* > > > > } > > > > public static void print(LinkedList list) { > >System.out.println("Contents of list: " + list); > > > > } > > > > On Mon, Jan 30, 2012 at 11:41 AM, Mihir Kulkarni >wrote: > > > > > > > > > I only intend to print the root to leaf paths. The correct output > should > > > be: > > > 721 > > > 725 > > > It works fine when I use array instead of LinkedList. > > > > > cheers, > > > Mihir Kulkarni > > > Graduate Student > > > University of California, Irvine > > >http://goo.gl/CvRcG > > > > > On Sun, Jan 29, 2012 at 10:06 PM, Rujin Cao > wrote: > > > > >> Is the correct output 7 2 1 5 ? > > > > >> Did you intend to print the leaf node ? > > > > >> -- > > >> 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. > > > > > -- > > > 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.- Hide quoted text - > > > > - Show quoted text - > > -- > 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. > > -- 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.
[algogeeks] Re: JAVA: Print all paths from root to leaf
Right idea. But you only need to remove the last item once, right at the end of the function. Don On Jan 30, 1:01 am, atul anand wrote: > @Mihir : actually you are using linked listso you are keep on adding > the nodes but not removing it..hence...you are getting wrong output.. > > i guess this should be done to fix the code. > > public static void paths(Node node, LinkedList list) { > if(node == null) return; > list.add(node.data); > > if(node.left == null && node.right == null) { > print(list); > } > else { > paths(node.left, list); > > * removeLastNodefromLinkedList();* > paths(node.right, list); > > * removeLastNodefromLinkedList();* > > } > > r*emoveLastNodefromLinkedList();* > > } > > public static void print(LinkedList list) { > System.out.println("Contents of list: " + list); > > } > > On Mon, Jan 30, 2012 at 11:41 AM, Mihir Kulkarni wrote: > > > > > I only intend to print the root to leaf paths. The correct output should > > be: > > 721 > > 725 > > It works fine when I use array instead of LinkedList. > > > cheers, > > Mihir Kulkarni > > Graduate Student > > University of California, Irvine > >http://goo.gl/CvRcG > > > On Sun, Jan 29, 2012 at 10:06 PM, Rujin Cao wrote: > > >> Is the correct output 7 2 1 5 ? > > >> Did you intend to print the leaf node ? > > >> -- > >> 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. > > > -- > > 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.- Hide quoted text - > > - Show quoted text - -- 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.