Look for CONNECT BY statement in Oracle doc, you can specify PRIOR id to link to row before current one.