It is leetcode #200, number of islands. My code is It works fine. But when I change the bfs function into It gives me the Time Limit Exceeded error, what could the reasons be? I think these two codes are nearly identical? Answer You are visiting the same state many times: Change to the following: