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, LinkedList<Integer> 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(LinkedList<Integer> list) {
System.out.println("Contents of list: " + list);
}
-----------------------------------------------------------------------------------------------
Regards
Vaibhav Mittal
NSIT, Dwarka
On , Mihir Kulkarni <[email protected]> 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 [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 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.