Follow the approach used for DFS.