Since I don't think depth first vs breadth first matters, maybe use removeLast which most likely is O(1) instead of O(n).