In recent years, Network Coding (NC) has been used to increase performance and efficiency in Wireless Sensor Networks (WSNs). In NC, Sensor Nodes (SNs) of network first store the received data as a packet, then process and combine them and eventually send them. Since the bandwidth of edges between SNs is limited, management and balancing bandwidth should be used for NS. In this paper, we present an optimization model for routing and balancing bandwidth consumption using NC and multicast flows in WSNs. This model minimizes the ratio of the total maximum bandwidth to the available bandwidth in network's edges and we use the dual method to solve this model. We also use the Karush-Kuhn-Tucker conditions (KKT) to calculate a lower bound and find the optimal solution and point in optimization model. For this purpose, we need to calculate the derivative of the Lagrangian function relative to its variables, in order to determine the condition as a multi-excited multi-equation device. But since the solution of equations KKT is centralized and for WSNs with a large number of SNs, it is very difficult and time consuming and almost impractical, we provide a distributed and repeatable algorithm for solving proposed model in which instead of deriving derivatives, combination Sub-gradient method and network flow separation method are used, thus allow each SN locally and based on the information of its neighboring nodes performs optimal routing and balances bandwidth consumption in the network. The effectiveness of the proposed optimization model and the proposed distributed algorithm with multiple runs of simulation in terms of the number of Source SNs (SSNs) and Lagrange coefficient and step size have been investigated. The results show that the proposed model and algorithm, due to informed routing and NC, can improve the parameters of the average required time to find the route optimal, the total amount of virtual flow in network's edges, the average latency end-to-end of the network, the consumed bandwidth, the average lifetime of the network and the consumed energy, or not very weak compared to other models. The proposed algorithm also has great scalability, because computations are done distributed and decentralized, and there is an insignificant dependence between the SNs.