Algorithm to determine whether a given graph is a caterpillar tree
When considering such recognition algorithms, it is sometimes helpful to consider equivalent characterizations of the graph in question. Your observations and ideas are valid.

In other words, we can first prove that caterpillars are such trees that when you delete all the leaves and incident edges, you are left with a path graph. This immediately suggest a simple linear time algorithm: check that your input graph is a tree, remove all the leaves, and check that you have a path.

In fact, I suggest you divide your problem into 3 subtasks you can work on independently:


Implement a function that outputs YES/NO based on whether the input graph is a tree.
Implement a function that removes all the leaves and the edges incident to them from the input graph.
Implement a function that outputs YES/NO based on whether the input graph is a path.