# Print Paths from root to all leaf nodes in a binary tree.

Objective: Given a binary tree, Print paths from root to all leaf nodes

Input: A binary tree

Example:

Approach:

• Do the preOrder traversal.
• Store the current node in an array, say Path[] and maintain the length say pathLength.
• check if you are at leaf node (both the left and right child of parent will be null).
• If not then keep traversing by making recursive calls to root.left and root.right. Pass the path and pathLen as parameter so that at each level have its own copy of path and and its length.
• If you hit the leaf node then print the array.
• See Code

Complete Code:

 public class PrintAllLeafPaths { int [] paths = new int; int pathLength =0; public void printPaths(Node root, int [] path, int pathLen){ if(root==null){ return; } path[pathLen++]= root.data; if(root.left==null && root.right==null){ print(path,pathLen); } else{ printPaths(root.left,path,pathLen); printPaths(root.right,path,pathLen); } } public void print(int [] path, int pathLen){ for(int i=0;i

Output :

```1 2 4 7
1 3 6 8
1 3 6 9
```