Given a binary tree with n total nodes, each having some quantity of cookies summing up to n total cookies.
Task is to transform this into nodes with equal cookies (1 cookie per node) with minimum total cost of transfer, provided the cost of transfer is equal to the qty of cookie being transferred between the nodes itself.
Cookies can be transferred only between, a)Parent to child b)Child to parent
Example,
In the below example, first 1 cookie can be transferred from left child to parent with cost = 1 and then transferred to right child to make it equal on all node with an additional cost of 1. So minimum total cost is 2.
1 2 1
2 0 =====> 1 0 =====> 1 1
(given tree) (transformed tree)
Minimum cost of transfer = 2
Can we have an optimal (time) algorithm to solve this?