ByteDance interview question

BFS / DFS traversal, moderate difficulty