title: print all root to leaf paths in a binary tree
body: <p>i am trying to print all root to leaf paths in a binary tree using java. </p>
  
  <pre><code>public void printAllRootToLeafPaths(Node node,ArrayList path) 
  {
      if(node==null)
      {
          return;
      }
      path.add(node.data);
  
      if(node.left==null &amp;&amp; node.right==null)
      {
          System.out.println(path);
          return;
      }
      else
      {
          printAllRootToLeafPaths(node.left,path);
          printAllRootToLeafPaths(node.right,path);
      }      
  }
  </code></pre>
  
  <p>In main method:</p>
  
  <pre><code> bst.printAllRootToLeafPaths(root, new ArrayList());
  </code></pre>
  
  <p>But its giving wrong output. </p>
  
  <p>given tree:</p>
  
  <pre><code>   5
    / \
   /   \
  1     8
   \    /\
    \  /  \
    3  6   9
  </code></pre>
  
  <p>Expected output: </p>
  
  <p>[5, 1, 3]</p>
  
  <p>[5, 8, 6]</p>
  
  <p>[5, 8, 9]</p>
  
  <p>But the output produced:</p>
  
  <p>[5, 1, 3]</p>
  
  <p>[5, 1, 3, 8, 6]</p>
  
  <p>[5, 1, 3, 8, 6, 9]</p>
  
  <p>Can some one figure it out...</p>