title: Print all paths in an n-ary tree according to their count
body: <p>I have the following n-ary tree. The value in the nodes are of the form <strong>names {count}</strong>. The count value of any node is the number of paths that include the node.</p>
  
  <p><img src="https://i.stack.imgur.com/uUdqx.png" alt="enter image description here"></p>
  
  <p>I need to print all paths(from root to leaf) in the tree until the count of that node becomes zero. Once the count of the node is zero, then path only up to its predecessor is printed(provided their count is greater than 0)</p>
  
  <p>The 6 paths for the above graph would be <br/>
  3-5-1-2-4-6 <br/>
  3-5-1-2<br/>
  3-5-1-7<br/>
  3-5-2<br/>
  3-1-4<br/>
  3-7<br/></p>
  
  <p><strong>[EDIT]</strong> Tried the following. Called the function toString(indent,node) using a loop that runs root.count number of times. But don't know how to stop after a path(3-5-1-2-4-6) has no more childs. Instead it prints 3-5-1-2-4-6-7-2-1-4-7. </p>
  
  <pre><code>public String toString(String indent, MyTree node) {
         String output="";
         while(node.count&gt;0){
             //output = indent + node.toString() + "\n";
             output = indent + node.name;
             node.count--;
             //if(node.count==0) break;
             String childsOutput = "";
             for (MyTree child : node.childs) {
                 childsOutput += toString(indent + " ", child);
             }
             return output + childsOutput;
         }
  return output;
  }
  </code></pre>
  
  <p>Thanks in advance.</p>