Recently, a new technique called network coding has been introduced where it has numerous advantages on the computer networks. Network coding changes the traditional view of data transfer in store and forward paradigm and allows the nodes to store packets in their memory to combine with other packets, then, send to next nodes. In this paper, an optimization problem is proposed which balance memory utilization in network-coding-based-multicast routing of Wireless Sensor Networks (WSNs). In the large WSNs, solving this optimization problem in a centralized manner is impractical and almost impossible, therefore, by using sub-gradient and decomposition techniques, a distributed algorithm is presented in which the nodes make a decision based on local information. The performance evaluation of proposed mechanisms are carried by OMNETT++ simulator and the results show that the proposed model averagely decreases the end to end delay and buffer overflow by 17% and 95%, respectively. However, it increases the energy consumption and consequently decreases lifetime by 14%. The decentralized algorithm decreases buffer overflow 69% and has more delay by 11% than the optimization model, however, it can be utilized in large WSNs due to its distributed computations.