@Mihir : actually you are using linked list....so 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<Integer> 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<Integer> list) {
   System.out.println("Contents of list: " + list);
}



On Mon, Jan 30, 2012 at 11:41 AM, Mihir Kulkarni <[email protected]>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 <[email protected]> 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 [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.
>

-- 
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.

Reply via email to