Reported September 2026
Googletree

Minimum Root-to-Leaf Cut Cost

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The data structure here is a plain binary tree stored as parallel arrays, and the whole Google OA reported in September 2026 hinges on walking it bottom-up. You're given leftChild, rightChild and two cost arrays, and you need the cheapest set of edge cuts that separates node 0 from every original leaf. It's a tree DP in disguise, and it's short once you see it. If you blank on the recurrence during the live assessment, StealthCoder runs invisibly as a safety net and hands you the solution while you keep typing.

The problem

You are given a rooted binary tree whose edges have nonnegative integer costs. Choose a set of edges to cut so that the root is disconnected from every leaf of the original tree. Return the minimum possible sum of the cut-edge costs.
The tree is encoded by four arrays of equal length n. Node 0 is the root. For each node i, leftChild[i] and rightChild[i] are its child indices, or -1 when that child is absent. When a child is present, the matching entry in leftCost or rightCost is the cost of that edge; when it is absent, the matching cost is 0.
A leaf is determined from the original tree: it is a node with no children before any cuts. Cutting an edge may disconnect many original leaves at once. Nodes that become leaves only because of earlier cuts do not add new requirements.
If all four arrays are empty, or if the tree contains only its root, return 0.
Interview follow-up
The primary problem requires nonnegative edge costs. With negative costs, an optimal answer may cut extra edges even after a leaf is already disconnected, so the primary recurrence no longer applies unchanged.

Function
minimumRootLeafCutCost(leftChild: List<Integer>, rightChild: List<Integer>, leftCost: List<Integer>, rightCost: List<Integer>) → long

Examples
Example 1
leftChild = [1,3,5,-1,-1,-1,-1]
rightChild = [2,4,6,-1,-1,-1,-1]
leftCost = [4,1,6,0,0,0,0]
rightCost = [7,5,2,0,0,0,0]
return = 11
Below node 1, cutting both leaf edges costs 1 + 5 = 6, so cutting the root-to-1 edge for 4 is cheaper. Below node 2, the leaf edges cost 6 + 2 = 8, so cutting the root-to-2 edge for 7 is cheaper. The minimum total is 4 + 7 = 11.
Example 2
leftChild = [1,2,3,-1]
rightChild = [-1,-1,-1,-1]
leftCost = [8,3,5,0]
rightCost = [0,0,0,0]
return = 3
There is one root-to-leaf path. Cutting its middle edge, from node 1 to node 2, costs 3, which is cheaper than cutting either other edge.
Example 3
leftChild = [-1]
rightChild = [-1]
leftCost = [0]
rightCost = [0]
return = 0
The root-only case has no edge that can be cut, and the required result is 0.

Constraints
0 <= n <= 2147483647
All four arrays have length n.
When n > 0, the child indices form one valid binary tree rooted at node 0: every node other than 0 appears exactly once as a child, and every node is reachable from the root.
Each child index is -1 or lies between 0 and n - 1.
An absent child has matching edge cost 0. A present edge has cost between 0 and 2147483647, inclusive.
The sum of all edge costs is at most 2147483647.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a post-order recurrence. For a node v, define best(v) as the min cost to disconnect v from all leaves in its subtree. A leaf has no children, so best(leaf) is infinity, since there's nothing below to cut. For each child edge with cost c, the option is min(c, best(child)): either cut this edge, or pay to separate below. Then best(v) is the sum of the options over its present children. The answer is best(root), or 0 for an empty or root-only tree. Pitfalls: treating a leaf as zero cost, which makes everything free, and using plain recursion on a deep tree that overflows the stack, so go iterative. Use a 64-bit sum. Example 2 shows why: the chain picks the cheapest edge, 3. If you freeze on the recurrence in the live OA, StealthCoder is the hedge that gives you the working code.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Minimum Root-to-Leaf Cut Cost 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 StealthCoder

Related leaked OAs

⏵ The honest play

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 Root-to-Leaf Cut Cost FAQ

What's the trick in Minimum Root-to-Leaf Cut Cost?+

Bottom-up DP over the tree. For each child edge, take the min of cutting that edge or the child's own best cost. A leaf's best cost is infinity, so its parent edge must be cut. Sum the options across children. The root's value is the answer.

How hard is this Google OA question really?+

Medium. The recurrence is three lines once you define the state. The real difficulty is the edge cases: leaf handling, an empty tree, and the array encoding. If you've seen min-cut on a tree or tree DP before, it's quick.

Why does the leaf base case matter so much?+

A leaf has nothing below it to cut, so the only way to separate it is cutting the edge into it. Setting its cost to infinity forces min(edgeCost, infinity) to pick the edge. Setting it to 0 would wrongly make every answer 0.

Do I need recursion or can I go iterative?+

Either works, but n can be huge and a chain-shaped tree makes recursion deep. An iterative post-order with a stack, or processing a BFS order in reverse, avoids stack overflow. Store best values in a long array.

How do I prepare for this in 48 hours?+

Write tree DP on array-encoded trees until post-order feels automatic. Practice defining a state per node and combining the children's results. Then test your code on the three examples, especially the root-only case and the single-chain case.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it