TikTok interview question

DFS/BFS Leetcode Problem, easy to code