Wireless sensor networks are composed of energy-constrained nodes. Therefore, it is to design routing algorithms to optimize energy usage of nodes. In view of the maximizing network lifetime, the paper put forward an optimal routing algorithm based on the maximin model, defined data transmission matrix, and designed selection system of nodes for avoiding possible routing loops. Based on energy consumption for sending and receiving data and available residual energy of nodes, it designed mathematical programming model to find optimal routing, so as to optimize data sent routing paths and data sent volume and balance energy consumption of nodes. The simulation results showed that the algorithm can balance the energy consumption of nodes effectively and extend network lifetime.