Root-to-Leaf Cut
Asked at Google
Reported by the problem's creator, not verified
A tree contains n nodes numbered 0 to n - 1 and is rooted at node 0. Each node has at most two children, and edges[k] = [parent, child, weight] describes one directed edge from parent down to its child; the order of edges carries no meaning. A leaf is a node with no children.
You may delete edges. After the deletions, following the remaining edges downward from the root must not reach any leaf of the original tree. Deleting an edge costs its weight, and the total cost is the sum over all deleted edges. Return the smallest possible total cost. With n == 1 the tree has no edges, so the answer is 0.
Examples
Example 1
Input: n =
7, edges =[[0,1,9],[0,2,5],[1,3,2],[1,4,4],[2,5,1],[2,6,7]]Output:
11Explanation: Under node 1, cutting both leaf edges costs 2 + 4 = 6, cheaper than the edge 0 to 1 (9). Under node 2, cutting the edge 0 to 2 costs 5, cheaper than 1 + 7 = 8. The total is 6 + 5 = 11.