A tree is a special graph: fully connected, no cycles, exactly edges for vertices. DFS on trees is simpler than on general graphs. You do not need to worry about cycles destroying your algorithm.
However, in an undirected tree, there is still a technical "cycle" issue: if you are at vertex and came from vertex , there is an edge back to . Without tracking the parent, you would immediately recurse back. Solution: pass the parent vertex as a parameter and skip it when iterating neighbors.