[HL-17] Word ladder and clone graph - BFS
The new feature, mock interview, from Leetcode is really cool! It forces me to solve the problem within the time limitation.
I met a BFS problem “127. Word Ladder”. I was trying to use DFS to generate all possible transformation paths. I got “Time Limit Exceeded”. Then, I looked at this link, and realized that this should use a BFS method (using a queue).
The code is as follows:
There is another question “133 Clone Graph” about BFS.
I wrote an incorrect BFS to solve this question. Again, understanding BFS doesn’t mean that you can code it correctly (I forgot that we have to trace visited nodes while traversing). This answer is referenced from this link.