ArticlesTrees and graphs
Depth-first search
A stack, or recursion, finishes one branch before the others. It answers reachability, not fewest edges.
Depth-first search dives. From the current vertex you walk one neighbour all the way down, then you come back and try the next neighbour. The call stack can hold that dive, or you can push vertices onto an explicit stack. Both produce the same kind of order.
It answers "can I get there?" and "which vertices are in this connected piece?". It does not answer "what is the shortest path?". The first time you arrive is just the first branch that happened to find the target, not the branch with the fewest edges. Use breadth-first search when the question is about distance and every edge costs the same.
On a tree, the recursive walk that finishes one child before the other is depth-first too. Backtracking is the same shape again, on decisions you are generating rather than on edges you were given. Choose, dive, undo, try the next choice.