TikTok Interview Question

DFS Traversal problem to implement