I want to perform BFS on a tree, to find out a certain leaf, but the graph is dynamic in nature, when I land on a leaf and that leaf isn't the one I am looking for, then its children are computed from the leaf (The leaf is no longer a leaf it is a node). 

I tried two implementations , and both produced erronous results. I think the pointers are getting invalidated or this is an incorrect implementation. My code is as follows
int y=0;
while(graph.end() - graph.begin() < 262145  and   graph.end() - graph.begin() < y){
        if(found(graph[y])){
            clock2 = graph[y];
            break;
        }
        else{
        if(graph[y].b[0] < 4) graph.push_back(move1(graph[y]));
        if(graph[y].b[1] < 4) graph.push_back(move2(graph[y]));
    }
    y++;
}
and the next implementation was something like this
for(vector<foo> :: iterator i = graph.begin();i!=graph.end();i++){
    if(found(*i)){
        clock2 = *i;
        break;
    }
    else{
        if(i->b[0] < 4) graph.push_back(move1(*i));//move1 and move2 are
        if(i->b[1] < 4) graph.push_back(move2(*i));//functions of return type foo
    }
}
Both of these are causing the programme to crash. What is wrong with them, how to implement these? Please comment with additional queries.
 
    