Maximum Product of Splitted Binary Tree

MediumTreesdfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given the root of a binary tree with at least two nodes, delete exactly one edge to split it into two trees. The value of a split is the product of the two trees' node-value sums. Find the largest possible product over all edges, and return it **modulo** 10^9 + 7. Maximize the true product first and only then take the remainder; do not compare products after reducing them.

Examples

Input: root = [1,2,3,4,5,6]
Output: 110
Cutting the edge above node 2 gives sums 11 and 10.
Input: root = [1,null,2,3,4,null,null,5,6]
Output: 90
Cutting the edge above node 4 gives sums 15 and 6.

Constraints

  • 2 <= number of nodes <= 5 * 10^4
  • 1 <= Node.val <= 10^4

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More dfs problems