Given the root of a tree where each edge has an associated cost. Find the minimum cost to visit every node of the tree.
A recursive solution that came to my mind is:
- base case when node is a leaf return 0.
- for every child c of the node recursively compute the cost.
- add up all those cost and also add the edge cost twice from node to child twice(as we need to backtrack).
- subtract the edge cost of the child that has maximum cost.("greedy" - we don't want to backtrack from the child that has maximum cost).
Is this approach correct?
Is there a better way to solve the problem?