Determining if an undirected connected graph is minimally connected
If $G$ is an undirected graph, it's a standard lemma that the following are equivalent:


$G$ is a tree.
$G$ is connected and has no cycles.
$G$ is connected and the number of edges is one less than the number of vertices.


Therefore, yes, your solution is also correct.

(The authors' solution does have the benefit of giving you a relatively simple setting where you can think through the properties of depth-first search in a deep way.  There are other problems that aren't solved so easily, and where the best solution involves some other sort of modification to depth-first search.)