import java.util.ArrayList;
class GFG
{
static class Node
{
int data;
ArrayList<Node> child;
public Node(int x)
{
this.data = x;
this.child = new ArrayList<>();
}
};
static void printPath(ArrayList<Integer> vec)
{
for (int ele : vec)
{
System.out.print(ele + " ");
}
System.out.println();
}
static void printAllRootToLeafPaths(Node root, ArrayList<Integer> vec)
{
if (root == null)
return;
vec.add(root.data);
if (root.child.isEmpty())
{
printPath(vec);
vec.remove(vec.size() - 1);
return;
}
for (int i = 0; i < root.child.size(); i++)
printAllRootToLeafPaths(root.child.get(i), vec);
vec.remove(vec.size() - 1);
}
static void printAllRootToLeafPaths(Node root)
{
if (root == null)
return;
ArrayList<Integer> vec = new ArrayList<>();
printAllRootToLeafPaths(root, vec);
}
public static void main(String[] args)
{
Node root = new Node(1);
(root.child).add(new Node(2));
(root.child).add(new Node(3));
(root.child.get(0).child).add(new Node(4));
(root.child.get(1).child).add(new Node(5));
(root.child.get(1).child).add(new Node(6));
(root.child.get(1).child.get(1).child).add(new Node(7));
(root.child.get(1).child.get(1).child).add(new Node(8));
printAllRootToLeafPaths(root);
}
}