It is important that it removes up to and including the specified depth because the stack is used when doing depth first search. Each call to popUntil is expected to remove a layer. New nodes are then added to the parent which is the first one from the previous layer.