I am writing following code to realize dfs without skipping visited nodes. To end the loop, I will use depth. e.g. if depth equals to 3, then a tree branch has at most 3 nod