Amazon Interview Question

Q1 from tree and q2 from graph(dfs/dsu)