An optimal routing and data aggregation scheme for wireless sensor networks is proposed in this paper. The objective is to maximize the network lifetime by jointly optimizing data aggregation and routing. We adopt a model to integrate data aggregation with the underlying routing scheme and present a smoothing approximation function for the optimization problem. The necessary and sufficient conditions for achieving the optimality are derived and a distributed gradient algorithm is designed accordingly. We show that the proposed scheme can significantly reduce the data traffic and improve the network lifetime. The distributed algorithm can converge to the optimal value efficiently under all network configurations.