@JordanBelfort vector<int>() makes a new 1D vector with 0 elements. We make a new vector whenever the variable depth wants to update a non-existent row in ret, hence we append a brand new empty 1D vector to ret, so we can add our new int element to it.
Why do you say "might need more memory"? The usual BFS uses a queue, usually a LinkedList in Java, which is already improved by using ArrayList. level and nextLevel usually would be in the queue anyway.
Did you notice that your two for loops are the same? Is there a reason for doing it twice?