MediumNo. 1339

Maximum Product of Splitted Binary Tree — LeetCode 1339 Python Solution

Topics
Tree, Depth-First Search, Binary Tree
Reading time
5 min

The problem

Given the root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized. Return the maximum product of the sums of the two subtrees.

Example

Input
root = [1,2,3,4,5,6]
Output
110
Explanation
Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)

Python solution

PythonSolution
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxProduct(self, root: Optional[TreeNode]) -> int:
        def sum(root: Optional[TreeNode]) -> int:
            if root is None:
                return 0
            return root.val + sum(root.left) + sum(root.right)

        def dfs(root: Optional[TreeNode]) -> int:
            if root is None:
                return 0
            t = root.val + dfs(root.left) + dfs(root.right)
            nonlocal ans, s
            if t < s:
                ans = max(ans, t * (s - t))
            return t

        mod = 10**9 + 7
        s = sum(root)
        ans = 0
        dfs(root)
        return ans % mod

Complexity

Time
O(n)
Space · auxiliary
O(n)where n is the number of nodes in the binary tree

Pattern: Tree Traversal

Choose the order — preorder, inorder, postorder, level — and the problem solves itself. LeetCode 1339. Maximum Product of Splitted Binary Tree is filed here because LeetCode tags it Tree and Binary Tree, which is the vocabulary this hub collects.

The tree traversal guide has the Python template for the pattern and the 225 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0098Validate Binary Search TreeMedium
0099Recover Binary Search TreeMedium
0100Same TreeEasy
0101Symmetric TreeEasy
0104Maximum Depth of Binary TreeEasy
0110Balanced Binary TreeEasy

Frequently asked questions

How hard is LeetCode 1339. Maximum Product of Splitted Binary Tree?

LeetCode 1339. Maximum Product of Splitted Binary Tree is rated Medium on LeetCode.

What is the time complexity of LeetCode 1339. Maximum Product of Splitted Binary Tree?

The Python solution on this page runs in O(n).

What is the space complexity of LeetCode 1339. Maximum Product of Splitted Binary Tree?

The Python solution on this page uses O(n), where n is the number of nodes in the binary tree auxiliary space.

What topics does LeetCode 1339. Maximum Product of Splitted Binary Tree cover?

LeetCode 1339. Maximum Product of Splitted Binary Tree is tagged Tree, Depth-First Search and Binary Tree on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.