Use queue<T> to store the nodes in Breadth-first order.
queue<T>
To avoid visiting the same node twice, use a vector<bool> or unordered_set<T> to store whether the node has been visited.
vector<bool>
unordered_set<T>
Make sure marking the node as visited before the node is enqueued! Otherwise the same node might be added multiple times.
1311. Get Watched Videos by Your Friends (Medium)arrow-up-right
Last updated 5 years ago