Reddit Experience · Apr 2026

Traversal difference between directed graphs and undirected graphs

32 upvotes 3 replies

Interview Experience

I had solved leetcode 207 Course Schedule and now trying to solve 261 graph valid tree. both the questions are similar where you need to find if there is a cycle. 1) i understood the part where no cyc

Full Details

I had solved leetcode 207 Course Schedule and now trying to solve 261 graph valid tree. both the questions are similar where you need to find if there is a cycle. 1) i understood the part where no cycle in a directed graph can be a cycle in a undirected graph. i solved this by adding both the nodes pointing to each other(so for example if its [1, 0] - then i add 1 to 0 and 0 to 1 in the adjacency matrix) 2) now what i dont understand is why do i have to run dfs for all nodes in directed graph but not for undirected graph. is it because we dont know the parent node in directed graph so we cant traverse the entire graph from a single node, but for a undirected graph we can traverse the tree from a single node so we run dfs only once? or is it because of some other reason?

Free preview. Unlock all questions →

Topics

Trees Graphs