In the last example, the node
n(ls, n(cc, n(gp, n(bs,-1))))
clearly contains the information we need to recover the path. The following algorithm converts a node into a (reversed) path. Observe how its definition is almost completely independent of the manner in which nodes have been implemented.
node2path(N,[SN]) :- node_state_of(N,SN), node_parent_of(N,-1), !. node2path(N,[SN|Y]) :- node_state_of(N,SN), node_parent_of(N,P), node2path(P,Y).