Minimum Tree Value After Leaf Relocations
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem hangs on a rooted tree, and Google's June 2026 OA report makes that plain. You get a tree rooted at node 1, a budget of k leaf moves, and a weird-looking goal: minimize the sum of every node's subtree sum. Don't let the wording scare you. That total is just each node's value times its depth plus one. So every move is about dragging a heavy node closer to the root. If you've got the OA in a day or two, this is a tree DFS problem wearing a costume. StealthCoder sits invisibly as a safety net if you blank mid-assessment.
The problem
You are given an undirected tree with n nodes, rooted at node 1. Node i has the positive value values[i - 1]. You may perform at most k operations. In one operation: Choose a non-root leaf in the current rooted tree. Disconnect it from its parent and reconnect it as a child of any remaining node, including its former parent. A node that becomes a leaf after earlier operations may be chosen in a later operation. The root itself is never moved. After all operations, define the value of every node to be the sum of the original assigned values of all nodes in its final rooted subtree, including itself. Return the minimum possible sum of these final node values. The answer can exceed the range of a 32-bit integer. Function minimumTreeValue(n: int, k: int, values: int[], edges: int[][]) → long Examples Example 1 n = 4 k = 1 values = [5,1,4,2] edges = [[1,2],[2,3],[2,4]] return = 21 Before any move, the total is 25. Move leaf 3 directly under the root. Its depth decreases from 2 to 1, reducing the total by 4. The resulting minimum is 21. Example 2 n = 5 k = 3 values = [1,10,1,10,10] edges = [[1,2],[2,3],[2,4],[4,5]] return = 63 Move node 5, then the newly created leaf 4, and also move leaf 3, attaching each one to the root. The total decreases from 94 to 63. Example 3 n = 1 k = 0 values = [7] edges = [] return = 7 The tree contains only the root, so no operation is possible and its final subtree sum is 7. Constraints 1 <= n <= 1000 0 <= k <= n - 1 values.length == n 1 <= values[i] <= 10^9 edges.length == n - 1 Every edge is a pair [u, v] with 1 <= u, v <= n. edges forms a valid undirected tree.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Rewrite the objective first. A node's value counts once for every ancestor including itself, so the total equals the sum of values[i] * depth(i), with the root at depth 1. Moving a leaf directly under the root sets its depth to 2, which is the best any non-root node can get. Gain for a node is values * (depth - 2) when you move it. Run a DFS to get depths. The catch is the order: a node can only move after its children are gone, and moving a leaf doesn't change other nodes' depths except its own subtree, which is empty. So pick the k nodes with the biggest positive gain, but check that the parent-before-child constraint holds, since a deeper node is moved first and that's always allowed. Sort the gains, take the top k, subtract. Use 64-bit math. Verify with example 1: gain 4 * 1 = 4, giving 21. StealthCoder is your hedge if the reformulation slips away live.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Minimum Tree Value After Leaf Relocations cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Tree Value After Leaf Relocations FAQ
What's the trick in the Google Minimum Tree Value After Leaf Relocations problem?+
Rewrite the sum of subtree sums as the sum of value times depth. Once you see that, each relocation is a gain you can compute from the node's depth. Then it's a DFS for depths and a pick of the best k gains.
How hard is this OA question really?+
Medium. The code is short, but the reframing is the hard part. If you try to simulate moves and recompute subtree sums each time, you'll burn time. With n up to 1000, the depth-based formula is simple and fast.
Do I need to worry about overflow?+
Yes. Values reach 10^9 and depths reach 1000 across 1000 nodes, so the total blows past 32 bits. Use a 64-bit type for depth products, the total, and the gains. The problem statement warns about this directly.
Does the order of moves matter with k operations?+
Not for the result. Moving a leaf only changes its own depth since it has no descendants. A node that isn't a leaf yet can become one after its children move. So you can choose the top k gains without worrying about order.
How do I prepare for this in 48 hours?+
Practice DFS depth computation on a tree built from an edge list, then practice turning a nested-sum objective into a per-node contribution. Test the three given examples by hand, including the single-node case with k = 0.