Because trees have no cycles, you can simplify your DFS. You do not need a visited array. Instead, avoid going back to the parent node you came from.
Pass the parent as a parameter in your recursive function. When exploring neighbors, skip the parent. This guarantees you will not revisit nodes or get stuck in loops.
This small trick makes tree traversal cleaner and faster than general graph traversal. No extra memory for tracking visited nodes, only one pointer to the parent.