I am trying to print all paths from root to all leaves in n-ary tree. This code prints the paths to the leaves, but it also prints subpaths too.
For example, let's say one path is 1-5-7-11. It prints 1-5-7-11, but it also prints 1-5-7, 1-5, so on.
How can I avoid this printing subpaths ?
Here is my code in matlab
Thanks
stack=java.util.Stack();
stack.push(0);
CP = [];
Q = [];
labels = ones(1,size(output.vertices,2));
while ~stack.empty()
x = stack.peek();
for e = 1:size(output.edges,2)
if output.edges{e}(1) == x && labels(output.edges{e}(2)+1) == 1
w = output.edges{e}(2);
stack.push(w);
CP = union(CP,w);
break
end
end
if e == size(output.edges,2)
Q = [];
for v=1:size(CP,2)
Q = union(Q,CP(v));
end
disp(Q)
stack.pop();
labels(x+1) = 0;
CP = CP(find(CP~=x));
end
end