The reducing of energy consumption for various nodes in wireless sensor networks plays an important and essential role in the prolonging of the life of these networks. In order not to be the energy consumption in some node is very high and in others is less or very low, the choice of distribution algorithms of the nodes role, as a router node or terminal nodes, and switching between them, plays an important role in prolonging the lifetime of wireless sensor networks. This paper presents an algorithm for the distribution of WSN nodes roles, including allowing the applying of many tree patterns to a single network. This offers the potential to alter the network nodes roles centrally by coordinator and switching between these tree patterns whenever the need arises according to the indicators of energy consumption in the nodes. The results show that the use of the algorithm leads to a significant improvement in the network life ranges between 2 and 4 times, according to the allowing the nodes to sleep and wakeup, or not, for different transmission rates where the scenarios have been tested for ZigBee based wireless sensors networks using NS-2 simulator.