What you're describing sounds somewhat like pre-order tree traversal but in reverse, from path to tree, where pos represents depth. For simplicity imagine there is a hidden root node with pos = 0 at the beginning of each set of inputs.
Recursion would do that job but is also overkill. Simply storing references to ancestor nodes in a stack and looping over the elements should suffice. When pos increments by 1 (I am assuming that pos never increments by more than 1), push the current element onto the stack. When pos decrements, pop the difference off of the stack. When there is no change in pos append the element into the list of children of the top element on the stack.