Maximum Product of Splitted Binary Tree โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโ€”or scroll down to preview all options.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

Understanding Maximum Product of Splitted Binary Tree
Detailed explanation and reference materials
Problem Overview

๐ŸŒณ 1339. Maximum Product of Splitted Binary Tree

Difficulty: ๐ŸŸก Medium
Topics: Binary Tree, DFS, Tree DP


๐Ÿง  Problem Statement

You are given the root of a binary tree.
Your task is to split the tree into two subtrees by removing exactly one edge, such that:

๐Ÿ”น The product of the sums of the two resulting subtrees is maximized

โš ๏ธ Since the result can be very large:

  • First, maximize the product
  • Then return the result modulo \(10^9 + 7\)

โœ‚๏ธ How the Split Works

  • Removing one edge divides the tree into two valid subtrees
  • Each subtree has a sum of node values
  • You must find the split that gives the maximum possible product

๐Ÿ“Œ Example 1

Input

root = [1,2,3,4,5,6]

Explanation

  • Remove the highlighted edge
  • Subtree sums = 11 and 10
  • Product = 11 ร— 10 = 110

โœ… Output

110

๐Ÿ“Œ Example 2

Input

root = [1,null,2,3,4,null,null,5,6]

Explanation

  • Subtree sums = 15 and 6
  • Product = 15 ร— 6 = 90

โœ… Output

90

๐Ÿ“ Constraints

ConstraintValue
Number of nodes2 โ‰ค n โ‰ค 5 ร— 10โด
Node value1 โ‰ค Node.val โ‰ค 10โด

๐Ÿ’ก Key Insight

  • First compute the total sum of the tree
  • For every possible subtree:
    • Let its sum be subSum
    • Other subtree sum = totalSum - subSum
    • Product = subSum ร— (totalSum - subSum)
  • Track the maximum product
  • Apply modulo after finding the maximum

๐Ÿš€ This problem is a great example of using DFS + subtree sums efficiently!

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
binary-trees
dp
dfs
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/description

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly ๐Ÿงฉ

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

๐Ÿ”’ Locked steps cannot be moved. Only unlocked steps are draggable.

๐Ÿ”Š Enable sound for swap feedback and completion effects.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.