Hive (CA) interview question

Leetcode medium, got a dfs/bfs question