TikTok interview question

DFS Traversal problem to implement