Network alignments are extensively used for comparing, exploring, and predicting biological networks. Existing alignment tools are mostly based on isomorphic and homeomorphic embedding and require solving a problem that is NP-complete even when searching a match for a tree in acyclic networks. On the other hand, if the mapping of different nodes from the query network (pattern) into the same node from the text network is allowed, then trees can be optimally mapped into arbitrary networks in polynomial time. In this paper we present the first polynomial-time algorithm for finding the best matching pair consisting of a subtree in a given tree pattern and a subgraph in a given text (represented by an arbitrary network) when both insertions and deletions of degree-2 vertices are allowed on any path. Our dynamic programming algorithm is an order of magnitude faster than the previous network alignment algorithm when deletions are forbidden. The algorithm has been also generalized to pattern networks with cycles: with a modest increase in runtime it can handle patterns with the limited vertex feedback set. We have applied our algorithm to matching metabolic pathways of four organisms (E. coli, S. cerevisiae, B. subtilis and T. thermophilus species) and found a reasonably large set of statistically significant alignments. We show advantages of allowing pattern vertex deletions and give an example validating biological relevance of the pathway alignment.