In this paper, we address the problem of computing optimal transportation plans of natural gas by means of compressor stations in pipeline networks. This non-linear (non-convex) problem takes into account two types of continuous decision variables: mass flow rate through each arc, and gas pressure level at each node. Compressors consume fuel at rates depending on flow and pressure, and the problem is to assign values to these variables such that the total fuel cost is minimized. We propose a dynamic programming algorithm based on tree decomposition, which applies to a broader class of instances than currently available techniques can solve. Through computational experiments, we demonstrate that our algorithm is capable to solve several instances where previously suggested methods and commercially avialable solvers for non-linear optimization fail.