Multi-agent pathfinding problems involve finding plans for agents that must travel from their start locations to their targets without colliding. Recent work produced a number of algorithms to solve the problem as well as an ample supply of related theory. Most existing works, however, were either too inefficient for practical use or had very restrictive conditions. This work proposes a novel approach to solving Multi-agent pathfinding problems, based on tree decompositions, called Tree-based Agent Swapping Strategy (TASS). Using TASS we can guarantee that a problem will be solved in polynomial time if it meets a few weak conditions. The overall strategy decomposes graphs into trees, where sub-problems can be solved efficiently, and then a solution on the original graph is extracted. Experimental results showed that TASS can find solutions to multi-agent pathfinding problems on a highly crowded tree with 1000 nodes and 996 agents in less than 3 seconds. Further experiments compared TASS with other modern contending algorithms and the results were very favorable.