The electricity of sensor nodes in wireless sensor networks is very limited, so it is an important research topic to deploy the sensor nodes and cooperate with an efficient routing algorithm for extending the network lifetime. In the related research, LEACH routing algorithm randomly selects cluster heads in each round to form a cluster network, which may cause additional power consumption and inability to maintain the optimal routes for data transmission. The cluster allocation and routing algorithm proposed in this study is based on the cluster architecture of LEACH, and the objective is to produce clusters with more sensor nodes to balance energy consumption of cluster head. For indirect-transmission routing algorithms, the sensor nodes near the base station may consume more energy due to a larger amount of data transmission. Therefore, this study proposed to increase the node density near the base station during deployment to compensate for the requirement of high energy consumption. The experimental results show that the proposed algorithm based on node density distribution can efficiently increase the lifetime of wireless sensor networks.