ByteDance interview question

my research project 2 leetcode problem (dfs)