Here is the code I wrote to print all paths of a Binary tree from root to leaf:
public static void printRootToLeaf(Node1 root, List list) {
If you have the reference to parent node, following code can print the paths:
public void printPaths(Node n) {
if(n != null) {
if(n.left == null && n.right == null) { // found a leaf node, go up the tree
StringBuilder sb = new StringBuilder();
sb.append(" ").append(n);
Node p = n.parent;
while(p != null) {
sb.insert(0, " ").insert(1,p);
p = p.parent;
}
System.out.println(sb);
}
printPaths(n.left);
printPaths(n.right);
}
}