Re: [algogeeks] JAVA: Print all paths from root to leaf
@Mihir Just understood what you were asking... atul is nearly right. You got to remove the unused items from LinkedList after calling print(left,..) and print(right, ..), which might contain more than one item. Since I'm not a Java guy, I just wrote some snippet using F# to illustrate the idea. Hope it helps. type BinaryTree'a = | Node of BinaryTree'a * BinaryTree'a * 'a | None let rec PrintPath (root : BinaryTree'a) list = match root with | None - () | Node(left, right, value) - let list = value :: list PrintPath left list PrintPath right list if left = None right = None then printfn %A (List.rev list) let tree = Node(Node(Node(None, None, 1), Node(None, None, 5), 2), None, 7) let list = [] PrintPath tree list -- 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] JAVA: Print all paths from root to leaf
Although there is no pointer concept in java, but still underlying containers in java get passed through functions via there base address. What you want is to send a copy of your list again recursively. --- public static void paths(Node node, LinkedListInteger list) { if(node == null) return; list.add(node.data); if(node.left == null node.right == null) { print(list); } else { paths(node.left, list.clone()); paths(node.right, list.clone()); } } public static void print(LinkedListInteger list) { System.out.println(Contents of list: + list); } --- Regards Vaibhav Mittal NSIT, Dwarka On , Mihir Kulkarni mihirk...@gmail.com wrote: Hello,This method below is not giving correct paths. Can someone please tell me the mistake. 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); paths(node.right, list); } } public static void print(LinkedList list) { System.out.println(Contents of list: + list); } eg: 7 / 2 / \ 1 5 It prints: 7 2 1 7 2 1 5 cheers,Mihir Kulkarni Graduate Student University of California, Irvine http://goo.gl/CvRcG -- 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.
Re: [algogeeks] JAVA: Print all paths from root to leaf
+1 @atul anand On Mon, Jan 30, 2012 at 12:31 PM, atul anand atul.87fri...@gmail.comwrote: @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, LinkedListInteger 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(LinkedListInteger list) { System.out.println(Contents of list: + list); } On Mon, Jan 30, 2012 at 11:41 AM, Mihir Kulkarni mihirk...@gmail.comwrote: 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 drizzle...@gmail.com 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. -- 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. -- Thanks Regards Saurabh Yadav -- 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] JAVA: Print all paths from root to leaf
use array, not clear on JAVA front though, but logic is fine, pass the vector and index instead Best Regards Ashish Goel Think positive and find fuel in failure +919985813081 +919966006652 On Mon, Jan 30, 2012 at 11:04 AM, Mihir Kulkarni mihirk...@gmail.comwrote: Hello, This method below is not giving correct paths. Can someone please tell me the mistake. public static void paths(Node node, LinkedListInteger list) { if(node == null) return; list.add(node.data); if(node.left == null node.right == null) { print(list); } else { paths(node.left, list); paths(node.right, list); } } public static void print(LinkedListInteger list) { System.out.println(Contents of list: + list); } e.g: 7 / 2 / \ 1 5 It prints: 7 2 1 7 2 1 5 cheers, Mihir Kulkarni Graduate Student University of California, Irvine http://goo.gl/CvRcG -- 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.
Re: [algogeeks] JAVA: Print all paths from root to leaf
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.
Re: [algogeeks] JAVA: Print all paths from root to leaf
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 drizzle...@gmail.com 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.
Re: [algogeeks] JAVA: Print all paths from root to leaf
@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, LinkedListInteger 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(LinkedListInteger list) { System.out.println(Contents of list: + list); } On Mon, Jan 30, 2012 at 11:41 AM, Mihir Kulkarni mihirk...@gmail.comwrote: 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 drizzle...@gmail.com 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. -- 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.