Although many applications use battery-powered sensor nodes, in some applications battery-and mainspowered nodes coexist. In this paper, we present a distributed algorithm that considers using mains-powered devices to increase the lifetime of wireless sensor networks for such heterogeneous deployment scenarios. In the proposed algorithm, a backbone routing structure composed of mainspowered nodes, sink, and battery-powered nodes if required, is constructed to relay data packets to one or more sinks. The algorithm is fully distributed and can handle dynamic changes in the network, such as node additions and removals, as well as link failures. Our extensive ns-2 simulation results show that the proposed method is able to increase the network lifetime up to 40 % compared to the case in which battery-and mains-powered nodes are not differentiated.